dotnetN00b
dotnetN00b

Reputation: 5131

Proper way to apply selector to jquery object?

So most examples I've seen of jQuery selectors are like this:

$('#example tbody tr td:nth-child(1)').css('display', 'none');

But what if I already have this:

var exampleTable = $('#example tbody');

How do I properly apply ... tr td:nth-child(1)').css('display', 'none'); to exampleTable?

Upvotes: 2

Views: 36

Answers (3)

skarist
skarist

Reputation: 1030

and yet another way to do it:

$('tr td:nth-child(1)',exampleTable).hide();

Upvotes: 1

Jon
Jon

Reputation: 437854

For this specific example, with .find:

exampleTable.find('tr td:nth-child(1)').hide();

In general, using one or more of the tree traversal methods (there's a whole lot of them).

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

Try to use .find() at this context since it is used to look for descendant elements,

var exampleTable = $('#example tbody');
exampleTable.find('tr td:nth-child(1)').hide();

And as a side note, use .hide() instead of .css('display', 'none');

Upvotes: 3

Related Questions