0

This is simply for keeping code neater. In CSS I can group elements like this:

.element1,
.element2,
.element3,
.element4,
.element5,
.element6 {
 font-weight:bold;
}

is there anything similar in jQuery or would I have to set each separately.

$('.element1').css('font-weight', 'bold');
$('.element2').css('font-weight', 'bold');
$('.element3').css('font-weight', 'bold');
etc

I suppose I imagine something like

$('.element1', '.element2', etc).css('font-weight', 'bold);
byronyasgur
  • 4,627
  • 13
  • 52
  • 96

5 Answers5

6

Even more simple, you can use precisely the same selector in jQuery as you could in CSS:

$('.element1, .element2').css('font-weight', 'bold);
David Thomas
  • 249,100
  • 51
  • 377
  • 410
2

The simple answer is yes, you can do that. But group your selectors using CSS syntax.

$('.element1, .element2, .element3').css('font-weight', 'bold');
Horatio Alderaan
  • 3,264
  • 2
  • 24
  • 30
1
$('.element1, .element2, etc').css('font-weight', 'bold');

JQuery multiple selectors.

Vucko
  • 20,555
  • 10
  • 56
  • 107
1

Yes you can!

Except you want it like this: $('.element1,.element2').css('font-weight', 'bold);

Source: http://api.jquery.com/multiple-selector/

DrCorduroy
  • 92
  • 9
1

Use multiple selectors. Example:

$("div,span,p.myClass").css("border","3px solid red");
jgillich
  • 71,459
  • 6
  • 57
  • 85