25

Using jQuery I want to bind an existing function to a button. I've been through the documentation and have found the bind method but the examples on the jQuery site bind newly created functions where as I want to bind a function that's already defined, e.g:

function fHardCodedFunction(){
   //Do stuff
}

function fBindFunctionToElement(){
   $("#MyButton").bind("click", fHardCodedFunction());
}

Is this possible? Or am I going about this the wrong way?

1
  • Here is the related section from the jQuery API documentation: Multiple Selector Commented Jan 17, 2018 at 17:20

4 Answers 4

37

The plain fHardCodedFunction already refers to the function and the suffix () will just call it. So just pass the function instead of calling it and thereby just passing the return value:

function fBindFunctionToElement(){
   $("#MyButton").bind("click", fHardCodedFunction);
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Jack Mills: Then you will need to use an (anonymous) wrapping function like function() { return fHardCodedFunction("foo","bar"); } that you pass as callback function to bind.
This is almost what I'm trying to do (I'm using .click() instead, but as far as I can tell, it's the same). However, my bound function isn't working - does the bound function have access to the same variables as the click() function? Or maybe when using the syntax $("#mydiv").click(function(){ $("#mydiv").hide();}, there are hidden variables passed to the function?
If you want to pass a parameter, you can call the outside function inside of your anonymous function, and pass the parameters in that way.
4

Borrowing from the other posts, you can parameterize your event handler as follows:

function fHardCodedFunction(someValue) {
  alert(this.id + " - " + someValue);
}


function fBindFunctionToElement() {
  var someValue = "whatever";
  $("#MyButton").bind("click", 
       function() {
         fHardCodedFunction.apply(this, [someValue]);
       }
  );
}


$(document).ready
(
  function() {
    fBindFunctionToElement();
  }
);

I'm using apply here because in function fHardCodedFunction I'd like the this property to refer to the MyButton element. Note also that apply expects an array for the second parameter, which is why I've wrapped someValue in brackets.

You don't have to do it this way and can forget about this this property altogether if you prefer.

Comments

2

Yes you can bind methods that written somewhere else, but you should ignore the parentheses :

function fHardCodedFunction(){
   //Do stuff
}

function fBindFunctionToElement(){
   $("#MyButton").bind("click", fHardCodedFunction);
}

Comments

0

Since this answer is tagged with event-handling, I suspect it would be useful to talk about the events, too. Your immediate problem, as stated by others, is simply to use the defined function function, and not call function(). The parens indicate to call the function, whereas you're trying to specify the function to use.

So, how do you handle the event? You only need to define it as the first argument of your function...

function fHardCodedFunction(event){
    console.log(event);
}

function fBindFunctionToElement(){
    $("#MyButton").bind("click", fHardCodedFunction);
}

The api.jquery.com documentation for bind() indicate as much, too. I have also put together a very small, working demo, to demonstrate event-handling.

HTML...

<input type="text" id="text">
<div id="main">Results appear here.</div>

JavaScript...

function clickFunction(e) { return $('#main').html(e.which); }
$('#text').bind('keypress', clickFunction);

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.