jQuery .remove()
Learn all about the jQuery function .remove().
Similar to .empty()
, the .remove()
method takes elements out of the DOM. Use .remove()
when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .detach()
instead.
Consider the following HTML:
1
2
3
4
|
|
We can target any element for removal:
1
|
|
This will result in a DOM structure with the <div>
element deleted:
1
2
3
|
|
If we had any number of nested elements inside <div class="hello">
, they would be removed, too. Other jQuery constructs such as data or event handlers are erased as well.
We can also include a selector as an optional parameter. For example, we could rewrite the previous DOM removal code as follows:
1
|
|
This would result in the same DOM structure:
1
2
3
|
|