jQuery Basic

jQuery Home jQuery Introduction jQuery Getting Started jQuery Syntax jQuery Selectors jQuery Events

jQuery Effects

jQuery Show/Hide jQuery Fade jQuery Slide jQuery Animation jQuery Stop jQuery Chaining jQuery Callback

jQuery manipulation

jQuery Get/Set jQuery Insert jQuery Remove jQuery CSS Classes jQuery Style Properties jQuery Dimensions

jQuery advanced

jQuery Traversing jQuery Ancestors jQuery Descendants jQuery Siblings jQuery Filtering jQuery Ajax jQuery Load jQuery Get/Post jQuery No-Conflict

jQuery Exercises

jQuery Practice Examples jQuery Quiz

jQuery References

jQuery Overview jQuery Selectors jQuery Events jQuery Effects jQuery HTML/CSS jQuery Traversing jQuery AJAX jQuery Misc jQuery Properties

jQuery Remove Elements & Attribute

We will learn how to remove the HTML elements or its contents as well as its attribute from the document using jQuery.

Remove Elements/Content

There are mainly two jQuery methods to remove elements and content from existing HTML elements.

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

jQuery empty() Method

The jQuery empty() method removes all child elements as well as other descendant elements and the text content within the selected elements from the DOM.

Example

$("#box").empty();
Try it Yourself

jQuery remove() Method

The jQuery remove() method removes the selected elements from the DOM as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

Example

$("#box").remove();
Try it Yourself

Filter the Elements to be Removed

The jQuery remove() method also accepts one parameter, which allows you to filter the elements to be removed. The parameter can be any of the jQuery selector syntaxes.

The example: to removes all <p> elements with class="hello":

Example

$("p").remove(".hello");
Try it Yourself

This example removes all <p> elements with class="test" and class="demo":

Example

$("p").remove(".test, .demo");
Try it Yourself

Note: According to the W3C (World Wide Web Consortium) DOM specification, any string of text within an element is considered a child node of that element.

We will learn to add and remove CSS class in jQuery in next chapter Click