Alexander
Alexander

Reputation: 971

Jquery selector through variables

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

Answers (1)

Niles Tanner
Niles Tanner

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

Related Questions