X

LATEST #JQUERY NEWS

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.

Recently Published

article image
»

jQuery Snippets: How to Add/Remove Classes After Delay

In your projects, there can often be instances of wanting to change ...

article image
»

jQuery Snippets: Back to Top Button

One common way to use jQuery is to add a useful and functional ...

article image
»

jQuery .on() Method: the Issue of Dynamically Added Elements

The jQuery .on() method is the recommended way for attaching events ...

article image
»

How to Use jQuery to Test CSS3 Browser Support

The tricky thing about using CSS properties (especially newer ones ...

article image
»

jQuery Snippets: Check/Un-Check All Function

This easy-to-implement jQuery code snippet is super useful, and can ...

article image
»

Useful jQuery Code Snippets for Handling CSS

If you’re not already familiar with code snippets, they are ...

article image
»

Using Method Chaining in jQuery

Using method chaining in jQuery helps to ensure that your code stays ...

article image
»

jQuery’s .children() Method

Just like in CSS, it’s possible to use jQuery to select the ...

article image
»

jQuery’s .click() Method

With jQuery, it’s super easy to make something happen as a result ...