Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Table rows collection help

Reply
Thread Tools

Table rows collection help

 
 
Trevor
Guest
Posts: n/a
 
      06-19-2006
This works in Firefox, but not in IE.

for (var i in appraisertable.rows) {
var row = appraisertable.rows[i];
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?

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      06-20-2006
Trevor wrote:
> This works in Firefox, but not in IE.
>
> for (var i in appraisertable.rows) {
> var row = appraisertable.rows[i];
> 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[i]);
}
}
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[i];
if (row.id) alert(row.id);
}

--
Rob

 
Reply With Quote
 
 
 
 
Trevor
Guest
Posts: n/a
 
      06-20-2006
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[i];
> > 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[i]);
> }
> }
> 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[i];
> if (row.id) alert(row.id);
> }
>
> --
> Rob


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
table.rows vs. table.getElementsByTagName (+ a regex question) eBob.com Javascript 5 05-20-2008 10:58 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
script for moving rows up and down and traverse thru rows of HTML table Subba Rao via DotNetMonster.com ASP .Net 0 03-19-2005 06:46 AM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
Help ASP; get last inserted value from one table, insert multiple rows in another table. PT ASP General 1 10-07-2004 07:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57