Reputation: 12163
$("#action_button").click(function() {
How would I bind an onEnter event with a different selector to the same code above?
Upvotes: 0
Views: 1737
Reputation: 18568
move the logic to a common method and call that from everywhere.for onenter event,bind keyup and check for keycode 13.
$("#new_selector").keyup(function(event){
var keyCode = event.keyCode || event.which; // browser compatible
if(keyCode === 13){
do_something();
}
});
$("#action_button").click(function() {
do_something();
});
function do_something(){
//
}
Upvotes: 1
Reputation: 2311
did you mean add events on the same line chaining one selector? as far as i know you have to have a brand new statement. you could try:
$("#action_button").click(function() {...}).add('other-selector').onEnter... but i dont know if that works at all.
Upvotes: 0
Reputation: 707148
Create a named function and use that instead of an anonymous function.
$("#action_button").click(processAction);
$("#otherSelector").otherEvent(processAction);
function processAction() {
// your code here
}
Upvotes: 6