Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destroy a DOM element with jQuery?

Tags:

jquery

dom

People also ask

How do I remove something from jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

Which method removes all matched elements from the DOM?

The remove( expr ) method removes all matched elements from the DOM.

Which jQuery method is used to remove the child elements from the selected element?

jQuery remove() Method The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.


Is $target.remove(); what you're looking for?

https://api.jquery.com/remove/


If you want to completely destroy the target, you have a couple of options. First you can remove the object from the DOM as described above...

console.log($target);   // jQuery object
$target.remove();       // remove target from the DOM
console.log($target);   // $target still exists

Option 1 - Then replace target with an empty jQuery object (jQuery 1.4+)

$target = $();
console.log($target);   // empty jQuery object

Option 2 - Or delete the property entirely (will cause an error if you reference it elsewhere)

delete $target;
console.log($target);   // error: $target is not defined

More reading: info about empty jQuery object, and info about delete


You are looking for the .remove() function.

http://docs.jquery.com/Manipulation/remove