jQuery’s .remove() Method

Ever wanted to remove an HTML element from your code dynamically? There are many situations in which being able to do this easily would be useful, and with jQuery, it's completely doable using the .remove() method.

Here's how it works. Let's say you want to remove a div (.bye) when it's clicked on. The following could would execute that action:

$(".bye").click(function(){
   $(this).remove();
})

You can also use the method to remove all elements of a particular tag type, and then use the parentheses to pass certain parameters through so that only HTML elements of a particular tag type AND a particular class or ID will be removed. So for example, let's say that when .bye is clicked, you'd like to remove all <p> tags with a class of .delete-me. Here's how that cod should look.

$(".bye").click(function(){
   $("p").remove(".delete-me");
})

Don't forget that using the .remove() method will remove your selected element AND everything inside of it, so make sure to exercise caution so you don't accidentally delete anything you don't mean to.



Responsive Menu
Add more content here...