Change css of multiple elements

I just want to know if instead

$(".element1").css('background','#000'); $(".element2").css('background','#000'); 

You can concatenate multiple elements so that they are affected by a single command, or if there is a more efficient way:

 $(".element1",".element2").css('background','#000'); 
+4
source share
2 answers

CSS selectors always support unions:

 $(".element1, .element2").css('background','#000'); 

You can also add to jQuery element:

 $(".element1").add(".element2").css('background','#000'); 

All you need is a quick look at the documentation .

+14
source

$(".element1,.element2").css('background','#000'); will fulfill

+3
source

Source: https://habr.com/ru/post/1389191/


All Articles