Removing multiple items in jquery

In my current code, I like

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

Is there a way to remove multiple elements using remove() once?

+7
jquery
source share
4 answers

It is not limited to .remove() , but simply separates the selector with 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.

+12
source share

Separating multiple elements requires comma-separated multiple selectors . Try the following:

  $("#foo,#bar").remove(); 
+3
source share

To select multiple items in jQuery syntax

 $('#foo, #bar').remove(); 
+3
source share

Remove some empty div tags.

  $(".className1.className2").each(function() { var current_element = $(this); if(current_element.text().trim()==""){ current_element.remove(); } }); 
+2
source share

All Articles