0

I want to select a tbody tag with no id or class or tags or whatever...

In CSS I know that I can use the following to select it:

table#header_info > tbody

But how can I achieve the same thing in JavaScript / jQuery? What is the best way?

3
  • Would you not do the same with jQuery? $("table#header_info > tbody").yourOperations... Commented Feb 8, 2012 at 22:05
  • 1
    If you use jQuery only for selecting elements, remove that jQuery and use 4. But if you use jQuery on your page and you want to load 24KB script into you page the first one is the fastest. (but you could remove table). The second one is wrong, see my comment at Rick Burns answer. Commented Feb 8, 2012 at 22:14
  • Alright, then i will use the first one, because i already use JQuery. Thanks for the reply's guys Commented Feb 8, 2012 at 22:17

6 Answers 6

2

In jQuery:

var tbody = $('#header_info').children('tbody');

In MooTools:

var tbody = $('header_info').getChildren('tbody');

In DoJo:

var tbody = dojo.query('tbody', '#header_info');

In Raw JS:

var tbody = document.querySelectorAll("#header_info > tbody ");

In Raw JS old version:

var tbody = document.getElementById('header_info').getElementsByTagName('tbody');

And there are many many other JavaScript Selecting Engines and functions that you can use...

Sign up to request clarification or add additional context in comments.

Comments

2

Just like CSS, in jQuery...

var tbody = $('#header_info > tbody')

I removed the unnecessary table selector, but you can keep it if you want.


http://docs.jquery.com/Tutorials

Comments

1
var elem = document.querySelectorAll("table#header_info > tbody "); 

But this won't work on some of old browsers. You should use jquery

$("table#header_info > tbody")

Comments

1

Using plain JavaScript, you can use getElementsByTagName()

// This retrieves the first `<tbody>` found as a child of the node with
// id `header_info`
document.getElementById('header_info').getElementsByTagName('tbody')[0]

Comments

1

With jQuery you can do:

$("#header_info").children("tbody");

Or:

$("#header_info > tbody");

2 Comments

#header_info tbody or #header_info > tbody is a big difference. The first one selects all tbody tags that are descendant of #header_info and the second one selects only the tbody tags that are children of #header_info
@WouterJ Missed the parent/child operator in OP's example, edited my answer. Thanks!
0

This will select a tbody in a table with an ID of header_info, like your CSS above.

$("#header_info tbody")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.