17

I have the following two functions:

$("input").keypress(function(event) {
   if (event.which == 13) {
   //code        
   }       
});

$('#login_submit').click(function () {        
   //code                    
});

The code which is being used in the functions are EXACTLY the same code, basically code dublication. So i was wondering if there is a way to combine these functions with an OR statement??

2
  • 1
    Why not just place that code in a separate function? And call the function... Commented Feb 3, 2013 at 16:13
  • i need the same but with an AND logic Commented Jul 24, 2017 at 14:58

5 Answers 5

29

Create your own callback and pass that to the event handlers.

var callback = function() {...};

$("input").keypress(function() {
    if (event.which == 13) callback();
});

$('#login_submit').click(callback);
Sign up to request clarification or add additional context in comments.

2 Comments

I have used this solution and it works perfectly. Thanks David.
I needed to do another, second callback btn1.addEventListener('click', () => callb()) because otherwise I get Unhandled Promise Rejection: ReferenceError: Cannot access uninitialized variable. Welcome to the Promise jungle. Also, only callb does not help, I need the function brackets.
10

Add a class to your HTML

<input class="myClass">
<div id="login_submit" class="myClass" ></div>

Now you can write:

$(".myClass").bind("keypress click", function(){});

Or do this:

$("input").add("#login_submit").bind("keypress click", function(){});

Be aware that clicking on the input will also trigger this.

Comments

2

Why don't you do it like this?

$("input").keypress(function(event) {
    if (event.which == 13) {
        foospace.yourfunction();
    }       
});

$('#login_submit').click(function () {        
    foospace.yourfunction();                    
});

var foospace={}; 
foospace.yourfunction=function() { 
    alert("your code goes here!"); 
}

Edit:

The callback solution by @David is slightly more elegant.

Comments

1

I would chain the events like:

        var watchCurrentCursorPosition = function (){
            console.log("foo");
        }
        $("input").keypress(
            watchCurrentCursorPosition

        ).click(
            watchCurrentCursorPosition
        );

Comments

0

For those who still are looking for an answer to the @Sino's question.

The code which is being used in the functions are EXACTLY the same code, basically code dublication. So i was wondering if there is a way to combine these functions with an OR statement??

JQuery .on() method is the way to go.

Description: Attach an event handler function for one or more events to the selected elements.

So your code could go like this:

$("input").on("click keypress", function(event) {
  if (event.which === 13) {
    event.preventDefault();
    //code        
  }       
});

Comments

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.