kralco626
kralco626

Reputation: 8644

jquery detach with find

Why does this not work

$rows = $("tbody").detach().find("tr");

but this does?

$rows = $("tbody").find("tr").detach();

I think the second way gets the set of "tr"'s and uses some sort of .each to detach them. I would think it would be more efficient, if there is a way to make it work, to detach the whole body once, and then get the tr's from it...?

Upvotes: 2

Views: 757

Answers (2)

kralco626
kralco626

Reputation: 8644

$rows in fact does have the same value, however in the first example the tbody tag is missing from the DOM so you need to add it back in.

Note also that

$rows = $("tbody").detach().find("tr"); (around 100ms to detach and store 500 tr elements in an array)

is significantly more efficient than:

$rows = $("tbody").find("tr").detach(); (around 170ms to detach and store 500 tr elements in an array)

Upvotes: 1

Amareswar
Amareswar

Reputation: 2064

First one detatching on $("tbody") and second statement detatching on rows of the table.

Upvotes: 0

Related Questions