snakespan snakespan - 6 months ago 46
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);


add method to the rescue!

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