.remove( [selector ] )Returns: jQuery
Description: Remove the set of matched elements from the DOM.
version added:
1.0.remove( [selector ] )
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
| <div class="hello">Hellodiv>
<div class="goodbye">Goodbyediv>
|
We can target any element for removal:
This will result in a DOM structure with the element deleted:
1 2 3
| <div class="goodbye">Goodbyediv>
|
If we had any number of nested elements inside , 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
| $( "div" ).remove( ".hello" );
|
This would result in the same DOM structure:
1 2 3
| <div class="goodbye">Goodbyediv>
|
Examples:
Removes all paragraphs from the DOM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <title>remove demotitle>
<script src="https://code.jquery.com/jquery-3.5.0.js">script>
<button>Call remove() on paragraphsbutton>
$( "button" ).click(function() {
|
Demo:
Removes all paragraphs that contain "Hello" from the DOM. Analogous to doing $("p").filter(":contains('Hello')").remove()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <title>remove demotitle>
<script src="https://code.jquery.com/jquery-3.5.0.js">script>
<p class="hello">Hellop>
<button>Call remove( ":contains('Hello')" ) on paragraphsbutton>
$( "button" ).click(function() {
$( "p" ).remove( ":contains('Hello')" );
|
Demo:
Description
Remove parent element with element.parentNode.remove()
Demo Code
ResultView the demo in separate window
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
<table id="test">
<tbody>
<tr>
<td>one