Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a dom element from the document, but save it as a variable?

Is there a way to remove a dom element from the document, but save it as a variable? I'm guessing I have to save the clone as a var, and then remove the original?

Also, would such a technique store styles etc?

like image 223
Matrym Avatar asked Apr 30 '10 06:04

Matrym


People also ask

How do I remove a DOM element?

To remove an element from the DOM, you follow these steps: First, select the target element that you want to remove using DOM methods such as querySelector() . Then, select the parent element of the target element and use the removeChild() method.

How do you remove an element from a document body?

To remove the p element from the body , start by coding bodyElement , followed by the removeChild() instruction. Then, remove paragraph from bodyElement by coding paragraph between the parentheses.

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.

Which method removes all matched elements from the DOM?

The empty() method removes all child nodes from the set of matched elements whereas the method remove() method removes all matched elements from the DOM.


1 Answers

Yes, that's what you do.

var savedElement = document.getElementById('element_that_you_want_to_save');
savedElement.parentNode.removeChild(savedElement);

// savedElement will still contain the reference to the object,
// so for example, you can do:
savedElement.style.height = '100px';
document.getElementById('container').appendChild(savedElement);
// etc.
like image 118
Casey Chu Avatar answered Sep 26 '22 22:09

Casey Chu