Adding up values in a textbox

Im running a function when a page loads

function calculateTotal() {
	var Power = document.getElementsByClassName("power_required")[0].value;

which works as expected, the value of the element appears in the console. How do I grab all the values in the array and loop through it to get the sum of all the values?

Hi there lurtnowski,

does this help…

function calculateTotal() {
	var Power = document.getElementsByClassName( 'power_required' ), total = 0;
        for ( var c = 0; c < Power.length; c ++ ) {
	      total += parseFloat( Power[ c ].value );
	console.log( total );


1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.