jQuery API

.remove()

.remove( [selector] ) Returns: jQuery

Description: Remove the set of matched elements from the DOM.

  • version added: 1.0.remove( [selector] )

    selectorA selector expression that filters the set of matched elements to be removed.

Similar to .empty(), the .remove() method takes elements out of the DOM. Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .detach() instead.

Consider the following HTML:

<div class="container">
  <div class="hello">Hello</div>
  <div class="goodbye">Goodbye</div>
</div>

We can target any element for removal:

$('.hello').remove();

This will result in a DOM structure with the <div> element deleted:

<div class="container">
  <div class="goodbye">Goodbye</div>
</div>

If we had any number of nested elements inside <div class="hello">, they would be removed, too. Other jQuery constructs such as data or event handlers are erased as well.

We can also include a selector as an optional parameter. For example, we could rewrite the previous DOM removal code as follows:

$('div').remove('.hello');

This would result in the same DOM structure:

<div class="container">
  <div class="goodbye">Goodbye</div>
</div>

Examples:

Example: Removes all paragraphs from the DOM

<!DOCTYPE html>
<html>
<head>
  <style>p { background:yellow; margin:6px 0; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p> 
  how are 
  <p>you?</p>
  <button>Call remove() on paragraphs</button>
<script>
    $("button").click(function () {
      $("p").remove();
    });

</script>

</body>
</html>

Demo:

Example: Removes all paragraphs that contain "Hello" from the DOM. Analogous to doing $("p").filter(":contains('Hello')").remove().

<!DOCTYPE html>
<html>
<head>
  <style>p { background:yellow; margin:6px 0; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p class="hello">Hello</p>
  how are 
  <p>you?</p>

  <button>Call remove(":contains('Hello')") on paragraphs</button>
<script>

    $("button").click(function () {
      $("p").remove(":contains('Hello')");
    });

</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .remove() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .remove()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • Corey

    If you’ve got a container with a lot of child elements you had better empty the container element before removing it. Due to the way jQuery handles remove vs empty, empty is thousands of times faster, at least in this situation.

    So do this:

    $(‘#container’).empty().remove();

    …instead of this:

    $(‘#container’).remove();

    • Lbvf50

      I’ve made an experiment with container which has 66300 children, and it shows .empty().remove() is 2% slower then simply remove().

      I did not see thousand of time faster, in general bot ways spend the same time:
      empty().remove(): 1877 ms
      .remove: 1833 ms.

      P.S. I’ve created container div with 300 groups of P and 10 green divs. Where are in each green div located 10 white divs with p. i.e. 3000 green divs, 3000 white divs.

      To render this page my CPU spend 10 seconds with 96% of usage. CPU Celeron 1.60GHZ, RAM 1GB, System XP, browser FF 3.6.3. profile with firebug.

      • Lbvf50

        Uuups. I was looking at wrong column of profiler. Both ways spend fey ten one millisecond to remove container. So there is no actual difference, beside more time typing empty().

        • Lbvf50

          Second Ups, I was right at the first message. Sorry…

          • http://trk7.com/blog/ Kesavan Rengarajan

            I agree with coreyward above. Using empty() before remove() is much faster and safer. I am using the Thickbox plugin and had to display a table with around 7000 rows with 8 columns on each row , trying to close the overlay crashed the page in Chrome(5.0.375.86 beta) and was extremely slow in Firefox(3.6.3). I was able to solve this by using empty() before remove() in the bit of code that was causing the issue.

          • Hagay-Tech

            i agree, i have hundreds of div containing lots of lists, tables, css styling, buttons and all you need for good looking UI, and remove is so slowly vs. empty, in chrome you really lose interest, feels like installing some software instead of clicking a page button, to see the results…
            Coreyward, if you find the reason for that in jquery code tell me, i dont have enough time to do that myself for now, but maybe in the next coming weeks..
            All the best and have a great year!
            http://www.hagay-tech.co.il

          • maxcho

            hello ,Do you know ZK

      • Corey

        Do any of them have content? The situation I was using was in a social website feed. There were about 50 items in the feed, each containing dozens of nodes of varying types with a fair bit of content and CSS styles applied to them. When we would try to remove a single feed item with .remove() it was pretty slow. Using .empty().remove() it was extremely fast.

        I’d really be interested in seeing what specifically caused this. I might dive into the jQuery code in a bit and see if I can’t figure it out.

        • Lbvf50

          Hi. Here is the script I’ve used for testing http://dima.awardspace.biz/rezak/jquery_remove.

          If You want I can send you html file with div-container which has 66300 elements (the size of this file is 3.6 Mb)… and for removing container div in this file FireFox spends 1.8 seconds, this time dose not depends of the way you doing it.

      • http://www.javinto.nl Javinto

        Just under IE8 and Windows XP SP3: I had to replace just under 100 nodes. It took exactly 10 seconds!!!! using .remove().

        When I put in .empty().remove() the job was done within 1 sec.

  • http://www.wekadesign.co.nz Mike

    To remove an item from a SELECT list.

    $(“select#mySelect option[value='option1']“).remove();

    • Guest

      Continuing with that thought, to remove the selected items from a SELECT list,

      $(“select#mySelect option[selected='true']“).remove();

      • Hagay-Tech

        just used that in my jquery page,
        always looks eleghant, all the best man!

      • Paul Baylis

        Here’s another option

        $(‘#mySelect :selected’).remove();

  • manu

    It goes very slow if you use the optional parameter like $(‘div’).remove(‘.hello’);
    You should better use $(‘div.hello’).remove();

    • Jayse Brock

      I believe this is due to the fact that $('div').remove('.hello') iterates through the set of DIVs twice: once for the initial search and then again to filter by class name before removal. Using the latter method of $('div.hello').remove(), the set of DIVs are only searched once.

  • matt

    Anyone tried to use this html5 video content?
    It will work in web-kit browsers but firefox 3.6.10 wont remove the video…

  • http://twitter.com/teesed teesed

    I'm trying to remove an image that is within a div within a div. The inner div has no class or id on it. I do not have control over the internal source code but I do have the ability to add to the header and footer so I'd like to try to get this done with jQuery. I thought I could remove the div that has “morecolors” in it, which is the name of the image. But it's a no go. I created a div that has just the text “morecolors” and it is removed, but not the div with the image.

    I have a demo of this available at http://www.findmytee.com/jquer…

    Note also that when I was trying to get this to work and I took out the image tags but left the image name (morecolors.gif) it not only removed that div, but it also removed the outer one that contains the product image. I don't want that to happen – I just the “more colors” image to be gone.

    Any help would be greatly appreciated.

  • Fghdfgh

    fghdfghdfghdg

  • maxcho

    hello ,Do you know ZK

  • Mehah