2

I have a function object to which I want to pass an argument on click using jquery, but it's not working.

<script>
var item=232;
$(function(){
    $('div').click(fn_clik('Mike'))
})

var fn_clik = function(name){
        alert('Name is:' + name + ' item is:' + item)
}
</script>

I don't understand the concept behind it. Can anyone please explain where am I going wrong?

Austin Mullins
  • 7,307
  • 2
  • 33
  • 48
tintin
  • 5,676
  • 15
  • 68
  • 97
  • https://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1 – epoch Jul 16 '14 at 14:15
  • 1
    First things first. This sample is "wrong" because with `fn_clik('Mike')` you are invoking `fn_clik` on the spot, while what you really want is to arrange for it to be called later. This is crucial in order to understand the possible solutions. – Jon Jul 16 '14 at 14:19
  • @tintin Did you find your answer ? – user2226755 Jul 16 '14 at 15:05

5 Answers5

4

Have the function you are calling return a function with the name, passed by argument, in a closure:

$('div').click( fn_clik('Mike') );

var fn_clik = function(name){      
     return function() {
         alert('Name is:' + name + ' item is:' + item);
     }
}

Or have an anonymous function you pass wrap the original function called with the correct parameter:

$('div').click(function () {
    fn_clik('Mike')
});

A quick note, in your original code, you were calling the function 'fn_clik' instead of passing it as an object. So your code was evaluating to:

$('div').click( undefined );

This is because your original 'fn_clik' has no return value;

kbirk
  • 3,906
  • 7
  • 48
  • 72
4

If you write "func(...)" you call your function, you must to juste write function name.

Without argument

Like this :

$('div').click(fn_clik);

With argument

If you want get argument, use anonymous function like this :

$('div').click(function () {
    fn_clik('Mike')
});

See this and event variables.

The this variable return element node, and event return click event.

$("body").click(function (event) {
    if (this == $("body")[0]) {
        alert("self");
    }
});

Demo

user2226755
  • 12,494
  • 5
  • 50
  • 73
1

This is hownormally I do ,

<script>
    var item=232;
    $(function(){
        $('div').on('click',function(){
            fn_clik('Mike');
        });

    })

    function fn_clik(name){
            alert('Name is:' + name + ' item is:' + item)
    }
</script>

Thanks.

Fisher Man
  • 487
  • 1
  • 5
  • 14
1

You are calling the function and using the return value (undefined) when binding, instead of getting a reference to the function that the event can call.

Wrap the function call in a function expression, so that you get a reference to it:

$('div').click(function() { fn_clik('Mike'); });

You can also use the proxy method to create a function with the parameter bound to it:

$('div').click($.proxy(fn_clik, this, 'Mike'));
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0
var item = 232;

$(function(){   
    $('div').click(function(e){fn_click("Mike");});
})

var fn_clik = function(name){      
    alert('Name is:' + name + ' item is:' + item)
}
afaolek
  • 8,452
  • 13
  • 45
  • 60