Jquery binds the same event to 2 selectors

I have two classes: Class1 and Class2.

At the moment, I have 2 calls to functions that call the same MyFunction function:

$('.Class1').click(function () { MyFunction() });
$('.Class2').click(function () { MyFunction() });

How do I rewrite this to say "when a click on Class1 and Class2 calls MyFunction".

Thank.

+5
source share
2 answers
$('.Class1, .Class2').click(function () { MyFunction() })
+19
source

to associate the click event with two different classes

$('.Class1, .Class2').click()

this means that when pressed, the Class1 OR Class2event will light up.

+3
source

All Articles