Is there any way to execute same code for different elements on the page?
$('.class1').click(function() {
some_function();
});
$('.class2').click(function() {
some_function();
});
instead to do something like:
$('.class1').$('.class2').click(function() {
some_function();
});
Thanks
Make sure you put a space like $('.class1,space here.class2') else it wont work
We can code like following also, I have used blur event here.
Simply use
$('.myclass1, .myclass2, .myclass3')
for multiple selectors. Also, you dont need lambda functions to bind an existing function to the click event.I have a link to an object containig many input fields, which requires to be handled by the same event. So I simply use find() to get all the inside objects, that need to have the event
In case your objects are one level down the link children() instead find() method can be used.
Or:
I normally use
on
instead ofclick
. It allow me to add more events listeners to a specific function.Hope it helps!