Removing Elements from the Page

$("p").remove(); $("p").remove(".menu"); $("#myList").children("li").remove();

You remove elements from the web page using the .remove([selector]) method on the jQuery object. This method modifies the DOM and removes the nodes from the tree.

There are a few different ways to use the .remove() method. Calling .remove() with no parameters removes all elements in the jQuery object’s set. For example, the following statement removes all <p> elements:

$("p").remove();

You can also pass a selector into .remove(), which limits the removal to items in the set that match the selector value. For example, the following statement removes all <p> elements with class="menu":

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

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.