Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

temporarily removing and later reinserting a DOM element?

Tags:

Is there some jquery magic that will let me do the following:

[0- define some element in HTML (eg, a unchecked checkbox)]

1- update its DOM element by setting one of its attributes using .attr() (eg, by setting its "checked" attribute using .attr('checked', true) )

2- temporarily remove that element from the DOM

3- reinsert the original element into the DOM, while preserving all its properties (ie, so that it is checked as it was at the end of step 1-- NOT like it was when initially defined in the HTML)

The reason why I am interested in removing these elements from the DOM (rather than hiding them) is that I have noticed that it seems to improve performance a good bit. My page has three different "states" and only a third of the total number of DOM elements is needed in any given state. [I wish to keep it as a single page with different states rather than breaking it into three separate pages.]

Until now I had been removing and reinserting elements into the DOM by storing in a var the value of

$("#myElement").html() 

and then removing it, but now I noticed that upon reinsertion of that HTML into the DOM the changes made [in step 1] had been "undone".

Is there a way to do this -- to temporarily remove unneeded stuff from the DOM in a way that preserves all its properties for later reinsertion?

thanks for any insight,

lara

like image 826
laramichaels Avatar asked Jan 08 '10 08:01

laramichaels


People also ask

Which property should be used to remove an element from DOM?

The removeAttribute() method removes an attribute from an element.

How do you remove element from DOM Vue?

Normally you will have a list of items you are displaying with v-for in your table rows which are stored in an array. After your ajax call you can remove this item using this. items. $remove(item) and it will be automatically removed from where it is displayed in the DOM.


1 Answers

Six days after the question was answered jQuery released 1.4 which contains the detach method. Which does exactly what you're looking for.

var detached = $('#element').detach(); $('body').append(detached); 
like image 151
thegaw Avatar answered Sep 20 '22 13:09

thegaw