barkster Posted June 22, 2007 Share Posted June 22, 2007 I have a page where there could be any number of textboxes that are named textbox1, textbox2, textbox3, etc... Without knowing how many there on are the page how can I get all the textbox# values and add them together? Thanks Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 22, 2007 Share Posted June 22, 2007 var textboxes = document.getElementsByTagName('input'); var count = textboxes.length; var total = 0; for(var i = 0; i < count; i++){ if(textboxes.name.indexOf('textbox') > 0) total += textboxes.value; } alert(total); Quote Link to comment Share on other sites More sharing options...
barkster Posted June 22, 2007 Author Share Posted June 22, 2007 Cool that worked, though had to do it this way. For some reason when it was adding the values it was adding 2+3 as 23 instead of 5. Thanks var totalcomp = 0; var totalbill = 0; for(i=0; i<document.forms[0].elements.length; i++){ var n = document.forms[0].elements.name; if (n.indexOf("tbbtime")>=0) { var v = parseFloat(document.forms[0].elements.value); totalbill = totalbill + v; } else if (n.indexOf("tbctime")>=0) { var v = parseFloat(document.forms[0].elements.value); totalcomp = totalcomp + v; } } 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.