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
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of┬ácheckbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...