Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove multiple elements in jquery

Tags:

jquery

In my current code I have like

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

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

like image 407
Wesley Brian Lachenal Avatar asked Apr 16 '14 08:04

Wesley Brian Lachenal


3 Answers

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.

like image 79
h2ooooooo Avatar answered Oct 03 '22 03:10

h2ooooooo


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

 $("#foo,#bar").remove();
like image 37
Milind Anantwar Avatar answered Oct 03 '22 01:10

Milind Anantwar


To select multiple elements in jQuery, the syntax is

$('#foo, #bar').remove();
like image 3
Matt Avatar answered Oct 03 '22 03:10

Matt