Michael Grigsby
Michael Grigsby

Reputation: 12163

binding multiple events to one selector in jQuery

$("#action_button").click(function() {

How would I bind an onEnter event with a different selector to the same code above?

Upvotes: 0

Views: 1737

Answers (3)

dku.rajkumar
dku.rajkumar

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

loki
loki

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

jfriend00
jfriend00

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

Related Questions