Reputation: 971
How can I achieve the following:
$('#someId, #anotherId').addClass('newKlass');
Using variables, without having to write a new line for each .addClass
action. I do not want to have to do the following:
firstEl = $('#someId');
secondEl = $('#anotherId');
firstEl.addClass('newKlass');
secondEl.addClass('newKlass');
Upvotes: 0
Views: 37
Reputation: 4021
make an array!
var elms = ["#this","#that"];
elms.forEach(function(elm){
$(elm).addClass('blue');
})
example: http://codepen.io/nilestanner/pen/OXZAjQ
Upvotes: 2