Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Table rows collection help (http://www.velocityreviews.com/forums/t925385-table-rows-collection-help.html)

Trevor 06-19-2006 08:04 PM

Table rows collection help
 
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?


RobG 06-20-2006 12:24 AM

Re: Table rows collection help
 
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


Trevor 06-20-2006 12:31 PM

Re: Table rows collection help
 
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




All times are GMT. The time now is 06:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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