Jump to content

math inside dynamically created text boxes


flemingmike

Recommended Posts

hi, how can i collect all the vaues of all the ltotal text boxes created and add them together?

  var column5 = row_in_table.insertCell(5);
			column5.setAttribute("align", "center"); 
            var element5 = document.createElement("input");

            element5.setAttribute("name", "ltotal[]");
			element5.setAttribute("id", "ltotal" + '-' + i);
			element5.setAttribute("value", ""); 
			element5.setAttribute("size", "7"); 
			element5.type = "text";
			element5.setAttribute('onblur','filllabour(this.id)');

           column5.appendChild(element5);			

//counter + 1           
		   i += 1;

        }



// End Of Labour Table
</script>




<script>
//Calculate Labour
    function filllabour(aa) {
	
	//Parse aa
	
	var the_string = aa;
	var parts = the_string.split('-', 2);

		// After calling split(), 'parts' is an array with two elements:
		// parts[0] is 'left or -'
		// parts[1] is 'right of -'

	var the_text = parts[0];
	var the_num  = parts[1];
	
	//Parse aa

	var dblQuantity = document.getElementById('lqlabour-' + the_num).value-0;
	var curRate = document.getElementById("llabour-" + the_num).value-0;
	var curRateHelper = document.getElementById("lhelper-" + the_num).value-0;
	var dblMarkup = document.getElementById("lmarkup-" + the_num).value-0;
	var curTotal = parseFloat(dblMarkup) / 100.0;
	llinetotal = ((dblQuantity * curRate) + (dblQuantity * curRateHelper)) * (1 + curTotal);
	llinetotal = llinetotal.toFixed(2);
    document.getElementById("ltotal-" + the_num).value = llinetotal;
	//document.getElementById(the_num).value = ("test");
	//alert(the_num);
	
	document.getElementById("labourtotal").value = aa;
	
    }
//Calculate Labour
</script>
Link to comment
Share on other sites

You need to create a function which will locate all the desired textboxes and then loop through them totaling the values. Then just call that function from whatever events you want to trigger the summation. If you use jQuery, this would be fairly easy to do:

function sumInputs(){
   var total = 0;
   $('input[name="ltotal[]"]').each(function(idx,ele){
      total += +ele.value;
   });

   alert(total);
}
If you are not (and/or dont want to) use jQuery, you'd need to write a bit more code to locate the correct elements, and use a for loop to go through them. Something like document.getElementsByTagName('input') to get each input, then a for loop and test each input's name to see if it matches "ltotal[]" and if so, add it's value to the total.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.