4

I would like to compute sum of subtotals dynamically but i always get this error:

document.getElementById("item_subtotal[" + cnt + "]") is null

My javascript code:

function calculateTotalAll(numitems) {     
  var cnt = 1;
  var totalAmt = 0;

  while(cnt <= numitems) {       
    totalAmt = parseInt(totalAmt) + parseInt(document.getElementById('item_subtotal[' + cnt + ']').value);
    cnt++;
  }

  document.getElementById('order_total').value = parseInt(totalAmt); 
}
2
  • Can you post the html for the elements you are working with? Commented Oct 15, 2010 at 5:00
  • The last line doesn't need parseInt(); you need string() instead. Commented Oct 19, 2010 at 3:23

4 Answers 4

2

I would look if the id exist, ie

  while(cnt <= numitems) {
    var curItem = document.getElementById('item_subtotal[' + cnt + ']');
    if(curItem!=null){
        totalAmt = parseInt(totalAmt) + parseInt(curItem.value);
    }
    cnt++;
  }

Furthermore, I would use the Firebug extension for Firefox to look at what might have gone wrong:

  while(cnt <= numitems) {
    var curItem = document.getElementById('item_subtotal[' + cnt + ']');
    if(curItem!=null){
        totalAmt = parseInt(totalAmt) + parseInt(curItem.value);
    }else{
        console.log('Couldn\'t find element item_subtotal[' + cnt + ']');
    }
    cnt++;
  }
Sign up to request clarification or add additional context in comments.

Comments

0

You're pretty close but you need to check for fields with empty values instead of just assuming they contain numbers. It works with only minor modifications in this JS fiddle

Changed your function to this:

function calculateTotal(numitems) {
    var totalAmt = 0;

    for (var cnt = 1; cnt <= numitems; cnt++) {
        var subtotal = document.getElementById('item_subtotal[' + cnt + ']');
        if (subtotal.value === null || subtotal.value === '') {
            continue;
        }

        totalAmt += (subtotal.value * 1);
    }

    document.getElementById('order_total').innerHTML = totalAmt;
}

4 Comments

There's a problem with this line i guess: document.getElementById('item_subtotal[' + cnt + ']'); since it will prompt that if (parseInt(subtot.value) == '') is null. HELP
But if I test it with document.getElementById('item_subtotal[1]') it works okay
I'm not sure what you're saying. It's easy enough to check for null if you need to, but since you should know how many elements there are it doesn't seem particularly necessary.
I ran the program and there's an error: if (parseInt(subtot.value) == '') is null
0

The item hasn't been defined in your page - double check to make sure it's actually there in the source.

1 Comment

When I do example: alert(document.getElementById('item_subtotal[25]').value); will get the value
0

Your problem may be those square brackets.

From the html4 spec:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

1 Comment

square brackets are fine, and also regularly used for the benefit of php scripts

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.