Brian Coolidge
Brian Coolidge

Reputation: 4649

Remove multiple elements in jquery

In my current code I have like

$("#foo").remove();
$("#bar").remove();

Are there any way to remove multiple elements by using remove() once?

Upvotes: 16

Views: 21619

Answers (4)

Suresh
Suresh

Reputation: 59

Remove multiple empty div tags.

 $(".className1.className2").each(function() {
        var current_element = $(this);      

        if(current_element.text().trim()==""){
            current_element.remove();
        }       
  });   

Upvotes: 2

Matt
Matt

Reputation: 3120

To select multiple elements in jQuery, the syntax is

$('#foo, #bar').remove();

Upvotes: 3

h2ooooooo
h2ooooooo

Reputation: 39532

It's not limited to .remove(), but just separate the selectors by a comma:

$("#foo, #bar").remove();

Multiple Selector (“selector1, selector2, selectorN”) | jQuery API Documentation

Description: Selects the combined results of all the specified selectors.

jQuery( "selector1, selector2, selectorN" )

selector1: Any valid selector.

selector2: Another valid selector.

selectorN: As many more valid selectors as you like.

Upvotes: 23

Milind Anantwar
Milind Anantwar

Reputation: 82231

You need comma separated multiple selector to target multiple elements.Try this:

 $("#foo,#bar").remove();

Upvotes: 3

Related Questions