flemingmike Posted September 14, 2013 Share Posted September 14, 2013 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> Quote Link to comment Share on other sites More sharing options...
Stefany93 Posted September 15, 2013 Share Posted September 15, 2013 // referencing the text box var text_box = document.getElementById('text_box'); // getting the value. We use the annoying alert window method for testing purposes. alert(text_box.value); If that is what you are looking for... Quote Link to comment Share on other sites More sharing options...
kicken Posted September 16, 2013 Share Posted September 16, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.