What is the jQuery equivalent code for the following JavaScript that saves onclick events?
3 answers
Is it really required that you get the event handler from another object? That doesn't sound like a great idea to me. Your best bet would be to define a handler and assign it to both objects.
var clickHandler = function(e) { alert('click!'); };
$('#foo,#bar').click(clickHandler);
+6
a source to share
By simply adding the answer to Daniel Schaffer (+ 1'd), you can also include the definition of your "handler" click, for example:
$("#foo, #bar").click( function() {
alert( this.id + ' was clicked.' );
} );
The behavior should be the same, but depending on your coding style, you may prefer this (s).
+3
a source to share