Jump to content


Photo

SetAttribute Onchange or onclick


  • Please log in to reply
5 replies to this topic

#1 flemingmike

flemingmike

    Advanced Member

  • Members
  • PipPipPip
  • 469 posts

Posted 11 September 2013 - 07:50 AM

hello, anyone able to help me with why the following may not be working?

element6.setAttribute("onchange", "filllabour()"); 



 

 

here is my filllabour

<script>
//Calculate Labour
    function filllabour() {
        var txt6 = document.getElementsByName("lqLabour[]").value-0;
		var txt7 = document.getElementsByName("llabour[]").value-0;
		var txt8 = document.getElementsByName("lhelper[]").value-0;
        var markup = document.getElementsByName("lmarkup[]").value-0;
		var muresult = parseFloat(markup) / 100.0;
        document.getElementsByName("ltotal[]").value = ((txt6 * txt7) + (txt6 * txt8)) * (1 + muresult);


    }
//Calculate Labour
</script>

here is my full insert column

            var column6 = row_in_table.insertCell(4);
			column6.setAttribute("align", "center"); 
            var element6 = document.createElement("input");

            element6.setAttribute("name", "lmarkup[]");  //to set name of the text box
			element6.setAttribute("id", "lmarkup");
			element6.setAttribute("value", "30"); 
			element6.setAttribute("size", "7"); 
			element6.setAttribute("onchange", "filllabour()"); 
			element6.type = "text";

            column6.appendChild(element6);	




Edited by flemingmike, 11 September 2013 - 07:52 AM.


#2 flemingmike

flemingmike

    Advanced Member

  • Members
  • PipPipPip
  • 469 posts

Posted 11 September 2013 - 08:18 AM

here is something else im trying with no luck

 //to insert the sixth column (for textbox to accept age)
            var column5 = row_in_table.insertCell(5);
			column5.setAttribute("align", "center"); 
            var element5 = document.createElement("input");

            element5.setAttribute("name", "ltotal[]");
			element5.setAttribute("id", "ltotal");
			element5.setAttribute("value", ""); 
			element5.setAttribute("size", "7"); 
			element5.type = "text";
			element5.onclick = function(filllabour)
				{
					alert("Changed!");
				}

            column5.appendChild(element5);			
	
           

        }



// End Of Labour Table
</script>




<script>
//Calculate Labour
    function filllabour() {
        var txt6 = document.getElementsByName("lqLabour[]").value-0;
		var txt7 = document.getElementsByName("llabour[]").value-0;
		var txt8 = document.getElementsByName("lhelper[]").value-0;
        var markup = document.getElementsByName("lmarkup[]").value-0;
		var muresult = parseFloat(markup) / 100.0;
        document.getElementsByName("ltotal[]").value = ((txt6 * txt7) + (txt6 * txt8)) * (1 + muresult);


    }
//Calculate Labour
</script>


#3 nogray

nogray

    Advanced Member

  • Members
  • PipPipPip
  • 930 posts
  • LocationSan Francisco CA

Posted 11 September 2013 - 06:15 PM

onclick or onchange are events and should be added either by addEventListener (and attachEvent for old IE browsers) or as the following

 

element6.onchange = filllabour; // no parenthesis


NoGray.com


#4 flemingmike

flemingmike

    Advanced Member

  • Members
  • PipPipPip
  • 469 posts

Posted 11 September 2013 - 08:07 PM

its not populating the total text box.  here is the filllabour

<script>
//Calculate Labour
    function filllabour() {
	
        var txt6 = document.getElementsByName("lqlabour[]").value-0;
		var txt7 = document.getElementsByName("llabour[]").value-0;
		var txt8 = document.getElementsByName("lhelper[]").value-0;
        var markup = document.getElementsByName("lmarkup[]").value-0;
		var muresult = parseFloat(markup) / 100.0;
        document.getElementsByName("ltotal[]").value = ((txt6 * txt7) + (txt6 * txt8)) * (1 + muresult);
	
	

    }
//Calculate Labour
</script>

here is my dynamic add

 //to insert the sixth column (for textbox to accept age)
            var column5 = row_in_table.insertCell(5);
			column5.setAttribute("align", "center"); 
            var element5 = document.createElement("input");

            element5.setAttribute("name", "ltotal[]");
			element5.setAttribute("id", "ltotal");
			element5.setAttribute("value", ""); 
			element5.setAttribute("size", "7"); 
			element5.type = "text";
			element5.onchange = filllabour;

            column5.appendChild(element5);		


#5 nogray

nogray

    Advanced Member

  • Members
  • PipPipPip
  • 930 posts
  • LocationSan Francisco CA

Posted 12 September 2013 - 12:43 PM

getElementsByName returns an HTML collections (or array) so you can't just assign a value to it. If you only have one element with that name, you can use  

 

document.getElementsByName("ltotal[]")[0].value = '.....'; // note the [0];

 

Otherwise use getElementById (each element needs a unique id).


NoGray.com


#6 priyankagound

priyankagound

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 18 September 2013 - 04:50 AM

try document.getElementById

document.getElementById("ctl00_ContentPlaceHolder1_txtCountry").setAttribute("onchange", "javascript:get_states(this);");






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com