1

var abc = '5';
$('#test').click(meraFunc(abc));
function meraFunc(abc)
{
  alert(abc);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" id="test" value="testMe">

when I load the page, alert pops up showing '5' [I havent yet clicked the button]. And when I click the button, nothing happens. Is this the right way to pass the arguments in jQuery?

5 Answers 5

9

The problem is that on this line:

j('#test').click(meraFunc(abc)); 

You are calling meraFunc and then passing whatever it returns (undefined) to the .click() function. This is why the alert pops up immediately.

You need to pass a function reference to .click() which would look like this:

j('#test').click(meraFunc);   // no parens after meraFunc

But then how to pass parameters? Wrap the call in another function as follows:

j('#test').click(function() {
   meraFunc(abc);
});

Note that the anonymous function I've added also has no () parens after it so it doesn't get called immediately. Then when an actual click event occurs the anonymous function will be called and it will in turn call your function with a parameter as meraFunc(abc).

0
7

Wrap it with an anonymous function (jsfiddle demo):

j('#test').click(function() {
    meraFunc(abc);
});
1
  • Perfectoooo!.. Acceping in 10 mins.. [SO rules... :)]
    – Umesh251
    Commented Feb 21, 2012 at 5:21
0

@Umesh251: I am aware of the fact that you have got the valid solution from @heikkim but just want to explain the abnormal behavior of your existing code.

$('#test').click(meraFunc(abc));

You were getting an alert on page-load because you were not binding a click event to #test. Thats why there were no alert when you manually click the test button.

4
  • That's not quite right. It is calling meraFunc immediately due to the () after the function name, and then it passes the return value from meraFunc() (which happens to be undefined) to the .click() function. This does not invoke the click event, though obviously it doesn't bind a handler either since you can't bind undefined.
    – nnnnnn
    Commented Feb 21, 2012 at 5:59
  • @nnnnnn: thats what I said. Just forgot to mention that meraFunc(abc) does not return anything. What exactly happens here is it neither binds nor calls click event. jsfiddle.net/BPv3n/2
    – codef0rmer
    Commented Feb 21, 2012 at 6:28
  • The part I objected to was where you said it was "invoking the click event" (my emphasis).
    – nnnnnn
    Commented Feb 21, 2012 at 6:48
  • @nnnnnn: yeah, i should remove that line. Thanks.
    – codef0rmer
    Commented Feb 21, 2012 at 6:53
-1
Var abc= 5;
$('#test').click(function());
function meraFunc(abc)
{
    alert(abc);
}

enjoy

0
-1

Wrap meraFunc inside anonymous function.

  var abc = '5';

  $('#test').click(function(){
    meraFunc(abc)
  });

  function meraFunc(abc)
  {
    alert(abc);
  }
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.