Reputation: 16126
I need to select first table and then second table from the html in string. I know this can be done by selector :eq(0)
and :eq(1)
, but
var firstTable = $("table", "<table></table>").length;
firstTable == 0
. Why?
Upvotes: 0
Views: 84
Reputation: 359786
When you pass a second argument to jQuery()
(aka $()
), you are specifying a context to search within. That is, this:
$(selector, context);
is equivalent to this:
$(context).find(selector);
So, you could rewrite your "broken" code like this to show why it's not finding a table
element:
var firstTable = $("<table></table>").find("table").length;
...because .find()
selects descendant elements only.
Upvotes: 2
Reputation: 23943
Try it like this to illustrate the problem:
var firstTable = $("table", "<div><table></table></div>").length;
// returns 1
The search happens within the context argument.
Upvotes: 1