dlebowski Posted April 29, 2012 Share Posted April 29, 2012 Hello. I am not a java expert and therefore do not understand why I keeping getting NaN for the total on this? I understand that it is saying I am trying to add "non-number", but I can't figure out why this won't work. Any help would be appreciated great. Thanks <script type="text/javascript"><!-- function updatesum() { var elems = document.getElementsByClassName('toAdd'); var myLength = elems.length, total = 0; alert(myLength); for (var i = 0; i < myLength; ++i) { total += elems[i].value - 0; } document.getElementById('sum').value = total; } //--></script> <body> <div class="toAdd"> Enter a number: <div><input id="Quantity[0]" onChange="updatesum()" /></div> and another number: <div><input id="Quantity[1]" onChange="updatesum()" /></div> </div> Their sum is: <input id="sum" readonly style="border:0px;"> Quote Link to comment Share on other sites More sharing options...
smerny Posted April 29, 2012 Share Posted April 29, 2012 your "elems" var consists of the entire "toAdd" div.. there is only 1 so no point in looping it, also div doesn't have "value" (you were trying elems.value) take the class="toAdd" off of that div and put it on your inputs (which are the elements that have the values you want to add). Quote Link to comment Share on other sites More sharing options...
dlebowski Posted April 29, 2012 Author Share Posted April 29, 2012 Thank you. That took care of it. 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.