Search code examples
jquerycssreadability

Can I apply the same styling to two elements together in jQuery by grouping them like in CSS?


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);

Solution

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

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