8

I want to pass arguments to the click() handler function in the following case:

function edit_tab(val){
    var prev_tab = $("#current_tab").html();
    if (prev_tab == 1) {
        $('.edit-basic-info').click(a); // here I want to pass some argument "a".
    }
}

How can I pass the argument a in the above code? The way I am trying to do is not working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
  • 8
    http://stackoverflow.com/questions/3273350/jquery-click-pass-parameters-to-user-function, check it here ,and this will be more help full http://jsfiddle.net/GDGXA/ – Developer Aug 11 '15 at 10:33
  • 2
    I don't believe this is a duplicate - the other questions ask how to pass additional parameters to a named function, this on the other hand is asking how to pass additional parameters to an event handler – billyonecan Aug 11 '15 at 12:56
  • 1
    @billyonecan I don't see how this *isn't* a duplicate. Both questions ask how to pass additional arguments to the function being passed to the `.click()` function (in this question, the function is 'a' and in the original question it is named 'add_event'. (also note that this question was closed by a gold badge holder, I'm more inclined to trust their opinion over yours as to whether or not this is a duplicate) – cimmanon Aug 17 '15 at 12:04
  • Agreed, just because the intent is different doesn't negate the duplicate status. Both questions deal with how to pass custom parameters along with the click event. This is a duplicate. – rlemon Aug 17 '15 at 12:24
  • @billyonecan And what would be the point of passing parameter `a` to the click event if you're not intending to pass it to the function that handles the click event? – cimmanon Aug 17 '15 at 12:34
  • My apologies, it is indeed a duplicate. Looking at it again I've no idea why I thought it wasn't. – billyonecan Aug 17 '15 at 13:23

4 Answers4

9

You can use trigger()'s extraParameters to pass additional parameters to your handler:

From the trigger() documentation:

extraParameters Type: Array or PlainObject Additional parameters to pass along to the event handler.

Example (also from the documentation):

$( "#foo" ).on( "custom", function( event, param1, param2 ) {
  alert( param1 + "\n" + param2 );
});

$( "#foo").trigger( "custom", [ "Custom", "Event" ] );
billyonecan
  • 20,090
  • 8
  • 42
  • 64
3

Use a closure:

function doSomething(myParam) {
    return function(event) {
        // Use both myParam and event here
    };
}

And in your event handler:

$('#foo').on('click', doSomething('parameter')); // Works fine

The reason this works is that the call to doSomething('parameter') returns a function, the inner function is not executed until the click handler calls it, and it can use all of the parameters passed to it from the outside.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • But there is no way here to pass a separate value of the parameter on each click, depending on context. Each click may require a separate value of the parameter to be passed to decide how the event handler should handle the event. – Sunny Mar 05 '16 at 08:47
-1
    <script>
    function edit_tab(val){
                var prev_tab=$("#current_tab").html();
                if(prev_tab==1){
                    $('.edit-basic-info').bind('click', function () {  
                       fndosomething(a)
//pass value in that function and use it

     });
                }
    }
    </script>
Sameer
  • 705
  • 10
  • 25
-1

bit more you can do like this

var message = 'check'; 
$('button').click({msg: message},functionFoo);

function functionFoo(e){
 alert(e.data.msg);
}
Developer
  • 561
  • 7
  • 29