jQuery remove: Main Tips
- jQuery
.remove()
function deletes the selected element with all of its content. - It eliminates all events and data associated with the specified elements.
- To remove elements but keep their data and events, apply .detach() instead.
Usage of .remove()
If you wonder which jQuery method is used to remove selected elements, you should know there's more than one answer. However, only jQuery .remove()
gets rid of the selected elements including their children, nodes, text, events, and data.
See in the example below how jQuery .remove()
function is used to remove a link:
Follow this syntax to delete element jQuery has:
$("selector").remove(selectorExpression);
A selectorExpression
is an optional parameter used to filter the set of elements to be removed.
Tip: to remove descedants and string texts within elements, use .empty() instead of jQuery .remove().