• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

How do I get the number of rows of a table using jquery in an HTML 5 document?

Hi:

Totally new to the world of HTML 5.

I've got a table defined
===================
<table id="foo">
 <tr> ....</tr>
 <tr> ....</tr>
 <tr> ....</tr>
</table>

I can get the number of rows in the the table by:
=======================================
var rowLength = document.getElementById("foo").rows.length;


Why doesn't the following work?
==========================
var rowLength = $("#foo").rows.length;

Thanks,
JohnB
0
jxbma
Asked:
jxbma
  • 4
  • 3
  • 2
2 Solutions
 
leakim971PluritechnicianCommented:
$("#foo") return a jQUery object not a table element

Use of the following :
$("tr", "#foo").length
$("#foo").find("tr").length
$("tr", "#foo").size()
$("#foo").find("tr").size()

Test page : http://jsfiddle.net/E7vPE/1/
0
 
jxbmaSoftware ConsultantAuthor Commented:
Thanks.
OK. I see that this works.

Can you explain to how to read wht's going on in:
========================================
$("tr", "#foo")

Does this mean find all the <tr>'s as children?
It would help clear things up a bit if you could put that into words.

Thanks,
JohnB
0
 
jxbmaSoftware ConsultantAuthor Commented:
How would I use the same jquery syntax to add and remove rows?

Thanks,
JohnB
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
leakim971PluritechnicianCommented:
Yes, the second argument is the context where to search the first one
0
 
tap52384Commented:
jQuery
$("#foo tr").length;

"Rows" doesn't work because rows is not a valid jQuery property.
0
 
jxbmaSoftware ConsultantAuthor Commented:
1) So is there any difference between:
===============================

$("#foo tr")


AND


$("tr", "#foo tr")

2) How would I use the same jquery syntax to add and remove rows?
========================================================


Thanks,
JohnB
0
 
tap52384Commented:
The second one is looking for nested <tr> tags, so that's not what you want.

Essentially, if you want to add a <tr> to an existing table, you could do:

$("#foo").append("<tr><td>cell content</td></tr>");

To remove a row, try:

$("#foo tr").eq(index).remove();

You could rewrite the second selector like:

$("tr", "#foo")

to select all rows as well. The second parameter is "context", or where the element(s) can be found.
0
 
jxbmaSoftware ConsultantAuthor Commented:
Hey Folks:

Thanks so much for jumping on this and giving me answers quickly.

Thanks,
JohnB
0
 
tap52384Commented:
Oh, and to explain a little better why "rows" works in plain javascript (I didn't know that) and not on the jQuery object, is because again, "rows" is not a property of a jQuery object. You can, however, get an actual HTML element, like a row, like such:

$("#foo tr")[index]

where "index" is the actual row. This would not give you a jQuery element, but an actual row of the table. You could use regular javascript methods on this object and it should work, like:

$("#foo tr")[index].style.backgroundColor = "#ffffff";
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now