4

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!

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175

2 Answers2

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

http://docs.jquery.com/Selectors

SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
jon skulski
  • 2,305
  • 3
  • 18
  • 27
4

You can use

$('#element1, #element2').click();

Or

$('#element1').add('#element2').click();

Both methods are good

Ionuț Staicu
  • 21,360
  • 11
  • 51
  • 58