snakespan snakespan - 15 days ago 9
Javascript Question

Event listener for multiple elements, when stored as variables

Is there a way to execute the same code for multiple elements when they are stored as variables in Jquery?

For example:

$('.class1, .class2').on('click', some_function);


when stored as variables:

var class1 = $('.class1),
class2 = $('.class2);

$class1, $class2.on('click', some_function);

Answer

add method to the rescue!

$class1.add($class2).on('click', some_function);