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
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

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

Screen Shot 2017-04-23 at 10.15.41 AM
»

How to Use jQuery to Create a Sticky Navigation on Scroll

Creating “sticky” elements is something that can often be ...

article image
»

How to Use jQuery’s Keystroke Methods

jQuery has a few built-in keystroke methods that are all triggered by ...

article image
»

How to Change iFrame Styling Using jQuery

If you’ve ever had the displeasure of working with iFrames, ...

article image
»

How to Use jQuery to Create Blinking Text

Blinking text is one of those cool features that you never actively ...

article image
»

Convert Images to Black and White using jQuery

Converting an image to black and white is no longer a manual event. ...

article image
»

How to Disable Right-Click Menu Using jQuery

There can be dozens of reasons why you may want to disable right ...

article image
»

How to Optimize jQuery Code for Better Performance

jQuery is the most popular client side library, in fact almost every ...

article image
»

Using Head.js to Load All Your JavaScript

In the past few years, client side frameworks/libraries have become ...