0

my HTML code looks like this

    <TABLE style="" border="0" CLASS="rdThemeDataTable" id="dtWardData"
                cellspacing="0">
                <COL id="wrdActive"></COL>
                <COL id="wrdOccupied"></COL>
                <TR Row="1" CLASS="ThemeAlignCenter">
                    <TD id="wrdActive_Row1" style="width: 50px"
                        CLASS="rdThemeDataTableCell"><SPAN id="lblactive_Row1">4</SPAN></TD>
                    <TD id="wrdOccupied_Row1" style="width: 50px"
                        CLASS="rdThemeDataTableCell"><SPAN id="lblOccupied_Row1">4</SPAN></TD>
                </TR>
            </TABLE>

   <TABLE style="" border="0" CLASS="rdThemeDataTable" id="dtWardData"
                cellspacing="0">
                <COL id="wrdActive"></COL>
                <COL id="wrdOccupied"></COL>

                    <TD id="wrdActive_Row1" style="width: 50px"
                        CLASS="rdThemeDataTableCell"><SPAN id="lblactive_Row1">6</SPAN></TD>
                    <TD id="wrdOccupied_Row1" style="width: 50px"
                        CLASS="rdThemeDataTableCell"><SPAN id="lblOccupied_Row1">2</SPAN></TD>

                </TR>
            </TABLE>

    Repeat...

it goes on like that for another 10 or so tables, all in the same source. editting the html is out of the question because its generated by a third party tool. all i need is add a little script at the end to add all the values of lblactive_Row1 (4 and 6 in this example)

3
  • Do you have duplicate id's at your HTML? Commented Feb 3, 2011 at 14:49
  • edited original post - all i need is add a little script at the end to add all the values of lblactive_Row1 (4 and 6 in this example) Commented Feb 3, 2011 at 14:49
  • @kamaci yes all of them are lvlactive_Row1 Commented Feb 3, 2011 at 14:50

1 Answer 1

1

It is not recommanded to use duplicate ID use class instead

window.onload = function() {
    var data = document.getElementsByTagName('span');
    var result = 0;
    for (var i = 0; i < data.length; i++) {
        if (data[i].id == 'lblactive_Row1') {
            result += parseInt(data[i].innerHTML, 10);
        }
    }

    console.log(result);
};

Here is the jsfiddle

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

12 Comments

getting an error: data[i].getElementsByTagName("span")[0] is undefined
never mind its working now thanks. its working but the result is not a sum of those numbers it should resturn 4+6 =10
what is it printing out? its printing out 10 on my side. use window.onload i forget to add it earlier. check the revision
i modified it so it grabs all the span and then check the id. which would be the ideal way. i didn't know there are other tags within the td. let me know if this works
Don't forget to specify a radix of 10 for parseInt, otherwise any values beginning with 0 will be treated as octal :)
|

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.