jQuery .remove() removes the set of matched elements from the DOM.
Syntax and Description
.remove([selector])
removes the set of matched elements from the DOM.
selector (optional)
is a selector expression
that filters the set of matched elements to be removed.
Its return value is the jQuery object, for chaining purposes.
Remove just added
The following code adds span
to the div
element
when mouse hovering and then removes the just added
elements.
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!--from www . ja va 2 s.c o m-->
$("div").hover(
function () {
$(this).append($("<span> ***</span>"));
},
function () {
$(this).find("span:last").remove();
}
);
});
</script>
</head>
<body>
<body>
<div><h1>java2s.com</h1></div>
</body>
</html>
Remove what contains a certain value
The following code removes what contains Hello.
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!-- w ww . j a v a 2 s .c o m-->
$("p").remove(":contains('Hello')");
});
</script>
</head>
<body>
<body>
<p class="hello">Hello</p>
<p class="hello">java2s.com</p>
</body>
</html>