views:

22

answers:

1

Hello, I'm coding an application that adds rows to a table without refresh using javascript and jquery. In order to append to the table, I need to do a count of the rows that are currently in the table. I'm using this code...

var count = $('#columns tr.FIELD').length;

The code works fine in Firefox and Chrome, but I am required to build around IE7. Is there any reason that IE returns a count of 0 while this selector works fine in other browsers?

Thanks.

+2  A: 

I think IE7 might be inserting a hidden tbody tag in your table, which causes the selector to be incorrect.

Try $('#columns').find('tr.FIELD').length

More appropriately, make sure your table is semantically correct

<table>
<thead>[HEADER ROW]</thead>
<tbody>[CONTENT]</tbody>
</table>
Stefan Kendall
+1 - To simplify my life I use tbody tags in tables now, just to keep it consistent with IE. :)
James Black
This bit me the first time I built an incorrect table and needed to parse over it in IE. I was building the tables dynamically, which caused all kinds of headaches with the auto-table-building :P.
Stefan Kendall
IE isn't the only browser that inserts the TBODY element. **All** the major browsers, including Firefox and Chrome, do the same thing. Try it yourself in the console - `var d = document.createElement("div"); d.innerHTML = "<table><tr></tr></table>"; alert(d.innerHTML);`. Also, there's no `>` selector in the OP's question, so even with the TBODY element all TRs would be returned (the OP specifically states that IE7 returns 0 rows).
Andy E