🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

Code has been added to clipboard!

jQuery .remove(): Delete Elements With Their Content, Events and Data

Reading time 1 min
Published Jan 22, 2018
Updated Sep 27, 2019

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:

Example
$("button").click(() => {
    $("a").remove();
});

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().