YouTip LogoYouTip

Jquery Dom Remove

jQuery Remove Elements | Tutorial

Tutorial -- Learning is not just about technology, but also about dreams!

jQuery Tutorial

jQuery Tutorial jQuery Introduction jQuery Installation jQuery Syntax jQuery Selectors jQuery Events

jQuery Effects

jQuery Hide/Show jQuery Fading jQuery Sliding jQuery Animation jQuery Stop Animation jQuery Callback jQuery Chaining

jQuery HTML

jQuery Get jQuery Set jQuery Add Elements jQuery Remove Elements jQuery CSS Classes jQuery css() Method jQuery Dimensions

jQuery Traversing

jQuery Traversing jQuery Ancestors jQuery Descendants jQuery Siblings jQuery Filtering

jQuery Ajax

jQuery AJAX Introduction jQuery load() Method jQuery get()/post() Methods

jQuery Other

jQuery noConflict() Method jQuery JSONP

jQuery Examples

jQuery Examples

jQuery Reference

jQuery Selectors jQuery Event Methods jQuery Effect Methods jQuery HTML / CSS Methods jQuery Traversing Methods jQuery AJAX Methods jQuery Miscellaneous Methods jQuery Properties

jQuery Plugins

jQuery Validate jQuery Cookie jQuery Accordion jQuery Autocomplete jQuery Growl jQuery Password Validation jQuery Prettydate jQuery Tooltip jQuery Treeview

jQuery Add Elements

jQuery Get and Set CSS Classes

jQuery - Remove Elements


With jQuery, it is easy to remove existing HTML elements.


Remove Elements/Content

To remove elements and content, there are mainly two jQuery methods:

  • remove() - Removes the selected element (and its child elements)
  • empty() - Removes the child elements from the selected element

jQuery remove() Method

The jQuery remove() method removes the selected element and its child elements.

Example

$("#div1").remove();

Try it yourself Β»


jQuery empty() Method

The jQuery empty() method removes the child elements of the selected element.

Example

$("#div1").empty();

Try it yourself Β»


Filtering the Removed Elements

The jQuery remove() method also accepts an optional parameter, which allows you to filter the elements to be removed.

The parameter can be any jQuery selector syntax.

The following example removes all <p> elements with class="italic":

Example

$("p").remove(".italic");

Try it yourself Β»

jQuery Add Elements

jQuery Get and Set CSS Classes

5 Notes

  • #0 Beautiful Rabbit
    178***0565@qq.com 57 When using remove() with a filter, it cannot remove child elements that match the filter.

    $(document).ready(function(){
      $("button").click(function(){
        $("#div1").remove(".part");
      });
    });

    Try it yourself Β»

    Beautiful Rabbit 8 years ago (2018-07-10)

  • #0 Smile
    100***7329@qq.com 120 Explaining the above: If a child element matches the condition in the filter but the parent element does not, the child element that matches the condition will not be removed. That is, the condition in the filter only applies to sibling elements, not to child elements.

    Smile 8 years ago (2018-07-26)

  • #0 Cat in a Cage
    726***224@qq.com 138 Supplementing the above: The $(selector) syntax returns a list of elements. Treat $("#div1") as a list. The filter condition in remove() actually filters and removes elements within this list, and will not remove elements that are not in this list (child elements are not in this list).

    Cat in a Cage 8 years ago (2018-08-20)

  • #0 jamtoday
    jam***ay@qq.com 105 If you throw away a bottle of water, that's remove.
    If you pour out the water from a bottle, that's empty.

    jamtoday 6 years ago (2020-12-05)

  • #0 Shaddock
    576***634@qq.com 34 Supplementing the above: Therefore, you need to modify the selector to select all <p> elements inside id="div1", then filter out the <p> elements with class="part", and then remove those elements.

    $(document).ready(function(){
      $("button").click(function(){
        $("#div1>p").remove(".part");
      });
    });

    Shaddock 6 years ago (2021-01-14)

Click to Share Notes

Cancel

Write notes...

Image URL

Image Description

Image Size Γ—

Share Notes

  • Nickname (Required)
  • Email (Required)
  • Reference URL

Category Navigation

← Jquery Css ClassesJson Intro β†’