0

I have realised that there is a much simpler way of looking at this.

How would I add up all the fields within a form or another respectable body. There are a series of text fields that contain information some are dynamically loaded in but lets not worry about that for more.

Quite simply all the fields have a unique ID but with the same start structure in this case 'product_total_price_PRI_....' (whereby the ... is a unique number).

So my understanding of javascript is still in the reverse engineering side of things rather than building arrays and objects, so how would I add up all those fields with an onClick event for example. Something like onclick="total();"

Many Thanks,

3 Answers 3

1

something like this might work...

not sure if you mean by "adding" if you're expecting all to be numeric and results added or just grab all the results and add them to an array... but this should be easily modified to add numeric data instead.

HTML

<form>
    <input id="product_total_price_PRI_asdfsdf" /><br /><br />
    <input id="product_total_price_PRI_sdfg" /><br /><br />
    <input id="product_total_price_PRI_fgh" /><br /><br />
    <input id="product_total_price_PRI_4356" /><br /><br />
    <input id="product_total_price_PRI_dfghfh" /><br /><br />
    <input type="button" value="submit" onClick="getFields()" />
</form>

JavaScript (updated to just get sum of inputs) Example of update

function getFields() {
    var inputs = document.getElementsByTagName('input'),
        result = 0;
    for( var i = 0; i < inputs.length; i++ ) {
        if( inputs[i].id.indexOf('product_total_price_PRI_') == 0 ) {
            var num = parseFloat( inputs[i].value );
            //if it's a valid number add it
            if( !isNaN( num ) ) result += num;
        }
    }
    alert( result );
}

working example JSFiddle

Sign up to request clarification or add additional context in comments.

3 Comments

Very good. The purpose of this is to add the um for the purpose of a sum. So I assume we need a ParseFLoat in here somewhere. Where abouts would I put it. The sum should add up the value only so I have stripped the result line to result+= inputs[i].value
here is an update that adds the sum of the values in the textbox if it's a number jsfiddle.net/subhaze/szUey/30
@Robin: Updated my answer to fit your needs.
0

Something like the following:

function total() {
    var i=0;
    var total=0;
    while (true) {
        var field = document.getElementById("product_total_price_PRI_" + String(i));
        if (!field)
            break;
        total += Number(field.value);
    }
    return total;
}

Comments

0
function summaFormae( form ) {
  var sum = 0;
  for ( var i = 0; i < form.elements.length; i++ )
    sum += parseFloat( form.elements[i].value );
  return sum;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.