Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select more than one element by id?

I have two identical click events for two different elements that do not share a class, as such:

$("#element_1").click(function(){
  alert("hello world");
});

$("#element_2").click(function(){
  alert("hello world");
});

I am looking for a way to assign that same click function to both of them without externalizing the function or repeating it (as seen above). If the elements shared the same class, I would have done something like this:

$(".element_class").click(function(){
  alert("hello world");
});

but they do not. How do I achieve something like that in jQuery 1.3?

Thank you!

like image 827
Yuval Karmi Avatar asked Dec 04 '22 15:12

Yuval Karmi


1 Answers

$('#element_1, #element_2').bind('click', function() {...});

http://docs.jquery.com/Selectors

like image 181
jon skulski Avatar answered Dec 06 '22 04:12

jon skulski