Removing DOM Elements - J Query

Problem
You want to remove elements from the DOM.

Solution

The remove() method can be used to remove a selected set of elements and their children elements from the DOM. Examine the following code:

When the preceding code is loaded into a browser, the anchor elements will remain in the page until the JavaScript is executed. Once the remove() method is used to remove all anchor elements from the DOM, the page will visually contain only an <h3>element.It’s also possible to pass the method an expression to filter the set of elements to be removed. For example, our code could change to remove only anchors with a specific class:

Discussion

When using the jQueryremove() method, you need to keep two things in mind:

  • While the elements selected are removed from the DOM using remove(), they have not been removed from the jQuery wrapper set. That means in theory you could continue operating on them and even add them back into the DOM if desired.

  • This method will not only remove the elements from the DOM, but it will also remove all event handlers and internally cached data that the elements removed might have contained.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics