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

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

3 Answers 3

6

Create a named function and use that instead of an anonymous function.

$("#action_button").click(processAction);
$("#otherSelector").otherEvent(processAction);

function processAction() {
    // your code here
}
Sign up to request clarification or add additional context in comments.

3 Comments

Okay so I can now use multiple selectors for the same code? thanks man! Good info for sure! Happy New Years btw.
@MichaelGrigsby if jfriend00's answer solved your problem please 'accept' his answer.
I accepted @dku.rajkumar's response. It was the most helpful.
1

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(){
//
}

1 Comment

For sure. Gotta love the community. Hopefully soon I can help others in the community once I reach the level of mastery I'm shooting for.
0

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.

1 Comment

This is clearly not what the OP is asking about. The question says "different selector" and "same code". With this, you would get "same selector" and "different code".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.