snakespan snakespan - 1 year ago 87
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 Source

add method to the rescue!

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download