</>
Skip to content
jQuery lessons (19/39)

jQuery — Remove Elements

remove()

Remove elements from the DOM (and their data/events):

$("p").remove();
$(".old-item").remove();

remove() with Selector

$("li").remove(".inactive");

empty()

Remove child elements and content (keep the element):

$("div").empty();

detach()

Remove but keep jQuery data and events:

var $item = $("li").detach();
// Later...
$("ul").append($item);

Practical: Delete Button

$(".delete-btn").click(function() {
    $(this).closest("tr").fadeOut(300, function() {
        $(this).remove();
    });
});

remove vs empty vs detach

MethodElementData/EventsReturn
remove()DeletedDeletedNothing
empty()KeptChildren deletedNothing
detach()DeletedKeptjQuery object

Removing Specific Items

// Remove by content
$("li").filter(":contains('old')").remove();

// Remove all except first
$("li:gt(0)").remove();

Mini Practice

  1. Remove an element on click
  2. Empty a container's contents
  3. Detach and reattach an element
  4. Remove elements by filter

Up Next

Continue with CSS Classes — adding, removing, and toggling classes.

Related Topics

Frequently Asked Questions about Remove Elements

What is Remove Elements in jQuery?

Remove Elements is a fundamental concept in jQuery. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Remove Elements?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Remove Elements.

Why is Remove Elements important in jQuery?

Remove Elements is essential for jQuery development. Understanding this concept will help you write better code and solve real-world problems more effectively.