1

I am running through a few jQuery tutorials but I can not seem to find the awnser to this confusion.

<div id="container">
 <a href="#">Click Me</a>
 </div> 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
 <script>
 function doSomthing(){
 aMessage = message;
 console.log(this);
 console.log(aMessage);
 }
 $('a').on('click', doSomthing)
 </script>

Looking at the code above as a user clicks the anchor tag and the doSomthing function runs.

However what i dont understand is why, if the code is changed to the following (adding the parenthesis to doSomthing), the doSomthing function runs immediately without clicking the anchor tag.

$('a').on('click', doSomthing())

Further from this what happens if I would like to pass a parameter when running doSomthing on click. For Example

<div id="container">
 <a href="#">Click Me</a>
 </div> 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
 <script>
 function doSomthing(message){
 console.log(this);
 console.log(message);
 }
 $('a').on('click', doSomthing('face bash'))
 </script>

What I would normal expect is that after I have clicked the anchor tag the console would log 'this' and the passed string, however the doSomthing function is run as soon as the page loads and not when the anchor tag is clicked :S

How would you normal pass parameters to functions called through other functions :S?

asked Jul 1, 2012 at 12:30
2
  • 1
    Have a look at stackoverflow.com/questions/4897368/… Commented Jul 1, 2012 at 12:34
  • 1
    Whenever you put parenthesis () after a function, it is called. To assign a function as event handler, you have to pass function reference, and that is just the name of the function. Commented Jul 1, 2012 at 12:36

2 Answers 2

3

Maybe try

$('a').on('click', function(){
 doSomthing('face bash');
})

?

Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
answered Jul 1, 2012 at 12:31
Sign up to request clarification or add additional context in comments.

3 Comments

Are you not then wrapping a function within an anonymous function? is that not bad practise, or untidy?
@Ben_hawk: No, this is pretty much standard practice in JavaScript. If you don't have to reference the function anywhere else, using anonymous functions is even cleaner IMO.
It is untidy. You can un-anonymize the function with a descriptor: function() _doSomthingClosure{ doSomthing('face bash'); } However, it isn't bad practice per se. This kind of closure creation has been the de-facto norm for several years now.
0

You need to return a new function from the function:

 function doSomthing(message) {
 return function() {
 console.log(this);
 console.log(message);
 };
 }

Since you are passing the result of calling doSomthing to .click, the result must be a function not undefined.

$('a').on('click', doSomthing('face bash'))

Stays as it is.

Alternatively you could keep doSomthing in tact and just proxy (Currying is still undocumented and requires jQuery 1.6+) it:

 $('a').on('click', $.proxy(doSomthing, null, "face bash"))
answered Jul 1, 2012 at 12:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.