Jump to content

SetAttribute Onchange or onclick


flemingmike

Recommended Posts

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);	



Link to comment
https://forums.phpfreaks.com/topic/282071-setattribute-onchange-or-onclick/
Share on other sites

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>

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);		

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.