javascript - For Loop not working -
i want function add 2 values inputted form instead of adding values merges number. if input 2 , 2 comes 22, whereas want 4 outputted. take loop not wotking
<script> var calculate = function(){ var input = document.getelementsbytagname("input"); var length = input.length; (var = 0; < length; i++) { input[i] = input[i].value; input[i] = parseint(input[i]); } var total_living_room = input[0] + input[1]; document.getelementbyid("sh").innerhtml=total_living_room; } </script>
the problem is, getelementsbytagname()
returns nodelist , no array (see,e.g., mdn on this).
both behave similar in many ways, elements of nodelist can not changed in way do.
as solution parse values in second array , use that:
<script> var calculate = function(){ var input = document.getelementsbytagname("input"), length = input.length, inputvals = []; (var = 0; < length; i++) { inputvals.push( parseint( input[i].value, 10 ) ); } var total_living_room = inputvals[0] + inputvals[1]; document.getelementbyid("sh").innerhtml=total_living_room; } </script>
edit
Comments
Post a Comment