Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Location
    Denver, CO
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Looping through table rows, not changing value from InnerHTML

    I'm populating a table from my database using .Net and within this application, I am then comparing the value the user has entered to the first column of every table row in the pre-populated table.

    For some reason, when I loop through each row in the table and return the value in the first column...the value always stays the same when in fact i know its different by just looking at it on the screen and in the html source.

    I throw up an alert message for each time it loops, and it its looping the correct amount of times, just not returning a the correct value.

    Could someone please help me out here, please see code below.. HTML and Javascript...Thanks!

    HTML:
    Code:
    <table id="tblProducts" name="tblProducts">
                
                        <tr>
                            <td id="tdProdNum">
                                3882
                            </td>
                            <td>
                                This is a vendor test to see if it reads them
                            </td>
                            <td>
                                
                            </td>
                        </tr>
                    
                        <tr>
                            <td id="tdProdNum">
                                7883
                            </td>
                            <td>
                                Test 3
                            </td>
                            <td>
                                
                            </td>
                        </tr>
                    
                        <tr>
                            <td id="tdProdNum">
                                8663
                            </td>
                            <td>
                                test
                            </td>
                            <td>
                                
                            </td>
                        </tr>
                    
            </table>
    JAVASCRIPT:
    Code:
    function checkValidItem(num) {
        rows = document.getElementById('tblProducts').getElementsByTagName('TR');
        var IsValid = new Boolean(false);
        var col;
        var i=0;
        while (!IsValid  || i<rows.length) {
            col = rows[i].document.getElementById('tdProdNum').innerHTML
            confirm(col + ', ' + i);
       
            if (num == col) {
                IsValid = true;
            }
            i++;
        }
        return IsValid;
    }
    Last edited by xXSnowBoardXx; 06-01-2009 at 06:17 PM. Reason: RESOLVED

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    ID is a unique attribute. As such, you can't have more than one table cell with the same ID. document.getElementById catches only the first occurrence of the object with the ID sent to the function and you will always be getting the first table cell with that ID. Use class instead and the following code in your loop:

    Code:
    col = rows[i].getElementsByTagName('TD');
    for (var d = 0; d < col.length;d++) {
    	if (col[d].className == "tdProdNum") {
    		var contents = col[d].innerHTML;
    		confirm(d + ',' + contents);
    	}
    }
    See how that goes for you.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • Users who have thanked Eldarrion for this post:

    xXSnowBoardXx (06-01-2009)

  • #3
    New to the CF scene
    Join Date
    Jun 2009
    Location
    Denver, CO
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Works like a charm! I should have known that to begin with! Thanks!!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •