TExcelTAD
Home › jQuery › jQuery Remove Elements

jQuery Remove Elements

Removing content is just as common as adding it: deleting a list item, clearing search results, closing a message. jQuery has several methods that differ in what they remove and what they keep.

remove() and empty()

Method Removes Keeps
.remove() The element itself and its children, plus their jQuery data and events nothing
.remove(selector) Only matched elements that also match selector the others
.empty() All children (content) the element itself
.detach() The element, like remove()… …but keeps its data and event handlers so it can be re-inserted
.unwrap() The parent of each element the element itself
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #box { border: 2px dashed #0769ad; padding: 8px; width: 240px; min-height: 30px; margin-top: 8px; }
    #box p { margin: 4px 0; background: #eef5fb; padding: 4px; }
  </style>
</head>
<body>
  <button id="empty">empty()</button>
  <button id="remove">remove()</button>
  <button id="reset">Reset</button>
  <div id="box">
    <p>Paragraph one</p>
    <p>Paragraph two</p>
  </div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    const original = $("#box").html();
    $("#empty").on("click", function () {
      $("#box").empty();
      console.log("box exists:", $("#box").length, "| children:", $("#box").children().length);
    });
    $("#remove").on("click", function () {
      $("#box").remove();
      console.log("box exists:", $("#box").length);
    });
    $("#reset").on("click", function () {
      if (!$("#box").length) $("<div id='box'>").insertAfter("#reset");
      $("#box").html(original);
    });
  </script>
</body>
</html>

empty() leaves the dashed box on the page; remove() deletes it entirely.

Filtering what to remove

.remove(selector) filters the current set before removing. These two lines do the same thing:

js
$("li").remove(".done");
$("li").filter(".done").remove();

A to-do list with delete buttons

A typical pattern: each item has a ✕ button, and clicking it removes its own item. .closest("li") finds the item that contains the clicked button.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    ul { padding: 0; width: 260px; }
    li { list-style: none; display: flex; justify-content: space-between; padding: 6px 8px; margin: 3px 0; background: #f4f6f8; border-radius: 4px; }
    li.done span { text-decoration: line-through; color: #888; }
    li button { border: 0; background: none; color: #c0392b; cursor: pointer; }
  </style>
</head>
<body>
  <ul id="todo">
    <li><span>Buy milk</span><button>✕</button></li>
    <li class="done"><span>Water plants</span><button>✕</button></li>
    <li><span>Call Sam</span><button>✕</button></li>
    <li class="done"><span>Pay rent</span><button>✕</button></li>
  </ul>
  <button id="clear">Clear completed</button>
  <button id="all">Clear all</button>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    function count() { console.log("items left:", $("#todo li").length); }

    $("#todo").on("click", "button", function () {
      $(this).closest("li").fadeOut(200, function () {
        $(this).remove();
        count();
      });
    });
    $("#clear").on("click", function () {
      $("#todo li").remove(".done");
      count();
    });
    $("#all").on("click", function () {
      $("#todo").empty();
      count();
    });
  </script>
</body>
</html>

Tip: Fade or slide first, then .remove() in the callback. Removing immediately makes the item vanish abruptly.

detach(): remove now, put back later

.detach() takes elements out of the page but keeps their jQuery event handlers and .data(). Re-inserting a detached element restores it fully working. This is also a performance trick: detach a big element, change it heavily, then re-attach it, so the browser recalculates the layout only once.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #ad { background: #fff3cd; padding: 10px; width: 240px; border: 1px solid #e0b400; margin-top: 8px; cursor: pointer; }
  </style>
</head>
<body>
  <button id="toggle">Detach / re-attach</button>
  <div id="slot">
    <div id="ad">Click me — I count my clicks.</div>
  </div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#ad").data("clicks", 0).on("click", function () {
      const c = $(this).data("clicks") + 1;
      $(this).data("clicks", c).text("Clicked " + c + " time(s)");
    });

    let $saved = null;
    $("#toggle").on("click", function () {
      if ($saved) {
        $("#slot").append($saved);   // handler and data still work
        console.log("re-attached, clicks so far:", $saved.data("clicks"));
        $saved = null;
      } else {
        $saved = $("#ad").detach();
        console.log("detached; still in memory:", $saved.length === 1);
      }
    });
  </script>
</body>
</html>

Try replacing .detach() with .remove(): after re-attaching, the element appears but clicking it no longer counts, and its saved data is gone.

Removing other things

Method Removes
.removeAttr("title") An attribute
.removeClass("active") A CSS class
.removeProp("x") A custom property (not for native ones like checked)
.removeData("key") Data stored with .data()
.off("click") Event handlers
.unwrap() The parent element (children stay in place)

Test yourself

Which method removes an element's children but keeps the element?

  1. .remove()
  2. .detach()
  3. .empty()
  4. .unwrap()

What is special about .detach() compared to .remove()?

  1. It only hides the element
  2. It keeps jQuery data and event handlers for re-insertion
  3. It removes only the children
  4. It is slower but safer

Which line removes only list items with class done?

  1. $("li").remove(".done")
  2. $("li").empty(".done")
  3. $(".done").empty()
  4. $("li").detach()