Phillip Senn
Phillip Senn

Reputation: 47635

Detaching, manipulating, appending

I was reading jqFundamentals this weekend, and Rebecca Murphey talks about:

The $.fn.detach method is extremely valuable if you are doing heavy manipulation to an element. In that case, it's beneficial to $.fn.detach the element from the page, work on it in your code, and then restore it to the page when you're done.

I have a table sort like this, which I got from "Learning jQuery", page 140:

var rows = $table.find('tr:not(:has(th))').get();
rows.sort(function(rowA,rowB) {
...
});
$.each(rows, function(index,row) {
  $table.children('tbody').append(row);
});

I wonder if I should detach the table and reattach it?

Upvotes: 2

Views: 1933

Answers (1)

DigitalZebra
DigitalZebra

Reputation: 41573

Detaching an element removes it from the DOM. So, if you were to detach the the table element, the whole table would likely disappear or "flash" until you re-insert the table back into the DOM.

However, you may want to detach only the tbody element from the DOM and then reinsert it into the table when you are done sorting the rows.

Upvotes: 2

Related Questions