Table rows collection help

Discussion in 'Javascript' started by Trevor, Jun 19, 2006.

  1. Trevor

    Trevor Guest

    This works in Firefox, but not in IE.

    for (var i in appraisertable.rows) {
    var row = appraisertable.rows;
    alert(row.id);
    }

    IE displays "undefined" and gives the error "id is null or not an
    object." Like I said before, Firefox works great. What's my problem
    here?
     
    Trevor, Jun 19, 2006
    #1
    1. Advertising

  2. Trevor

    RobG Guest

    Trevor wrote:
    > This works in Firefox, but not in IE.
    >
    > for (var i in appraisertable.rows) {
    > var row = appraisertable.rows;
    > alert(row.id);
    > }
    >
    > IE displays "undefined" and gives the error "id is null or not an
    > object." Like I said before, Firefox works great. What's my problem
    > here?


    Guessing that appraisertable is a table or a tableSection element, then
    appraisertable.rows should return a collection. A for..in statement
    will iterate over the enumerable properties, if you look at what is
    returned by your code, Firefox goes through the indexes first, then the
    other properties (length, item, namedItem). IE returns the length
    first, so in the first iteration, 'row' is set to the value of the
    length property which is a number. Number objects don't have an id
    property (unless you've given them one) so it returns undefined.
    Firefox does too, but after returning the indexes. Try the following:

    <table id="tableA">
    <tr id="one"><td>one</td></tr>
    <tr id="two"><td>two</td></tr>
    <tr id="three"><td>three</td></tr>
    </table>

    <script type="text/javascript">
    function blah(){
    var r = document.getElementById('tableA').rows;
    for (i in r){
    alert( 'Property: ' + i + '\n'
    + 'Type: ' + (typeof i) + '\n'
    + 'Value: ' + r);
    }
    }
    blah();
    </script>

    In any case, there seems no point in using for..in - the usual reason
    is for sparse arrays, but that should never happen with a rows
    collection (they are 'live' and self-adjusting). Use:

    var row, rows = appraisertable.rows;
    for (var i=0, len=rows.length; i<len; i++) {
    row = rows;
    if (row.id) alert(row.id);
    }

    --
    Rob
     
    RobG, Jun 20, 2006
    #2
    1. Advertising

  3. Trevor

    Trevor Guest

    Thank you so much! That's precisely what I needed to know.

    RobG wrote:
    > Trevor wrote:
    > > This works in Firefox, but not in IE.
    > >
    > > for (var i in appraisertable.rows) {
    > > var row = appraisertable.rows;
    > > alert(row.id);
    > > }
    > >
    > > IE displays "undefined" and gives the error "id is null or not an
    > > object." Like I said before, Firefox works great. What's my problem
    > > here?

    >
    > Guessing that appraisertable is a table or a tableSection element, then
    > appraisertable.rows should return a collection. A for..in statement
    > will iterate over the enumerable properties, if you look at what is
    > returned by your code, Firefox goes through the indexes first, then the
    > other properties (length, item, namedItem). IE returns the length
    > first, so in the first iteration, 'row' is set to the value of the
    > length property which is a number. Number objects don't have an id
    > property (unless you've given them one) so it returns undefined.
    > Firefox does too, but after returning the indexes. Try the following:
    >
    > <table id="tableA">
    > <tr id="one"><td>one</td></tr>
    > <tr id="two"><td>two</td></tr>
    > <tr id="three"><td>three</td></tr>
    > </table>
    >
    > <script type="text/javascript">
    > function blah(){
    > var r = document.getElementById('tableA').rows;
    > for (i in r){
    > alert( 'Property: ' + i + '\n'
    > + 'Type: ' + (typeof i) + '\n'
    > + 'Value: ' + r);
    > }
    > }
    > blah();
    > </script>
    >
    > In any case, there seems no point in using for..in - the usual reason
    > is for sparse arrays, but that should never happen with a rows
    > collection (they are 'live' and self-adjusting). Use:
    >
    > var row, rows = appraisertable.rows;
    > for (var i=0, len=rows.length; i<len; i++) {
    > row = rows;
    > if (row.id) alert(row.id);
    > }
    >
    > --
    > Rob
     
    Trevor, Jun 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Subba Rao via DotNetMonster.com

    script for moving rows up and down and traverse thru rows of HTML table

    Subba Rao via DotNetMonster.com, Mar 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    8,278
    Subba Rao via DotNetMonster.com
    Mar 19, 2005
  2. Rio
    Replies:
    4
    Views:
    1,216
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    998
    Øyvind Isaksen
    May 18, 2007
  4. PT
    Replies:
    1
    Views:
    407
    Ken Schaefer
    Oct 7, 2004
  5. eBob.com
    Replies:
    5
    Views:
    501
    Martin Honnen
    May 20, 2008
Loading...

Share This Page