55

I want to pass few parameters to click() event in jQuery, I tried following but it's not working:

commentbtn.click(function(id, name){
    alert(id);
});

And also if we use bind then how we'll do that:

commentbtn.bind('click', function(id, name){
    alert(id);
});
3
  • 3
    What are id and name. Where do you get them from? Are the values at the time of the click or something initialised at the time you're binding? How will click know what to pass into the function? It will always pass the event object and nothing else. Commented Oct 22, 2010 at 6:50
  • 1
    They are initialized, before binding the event. Commented Oct 22, 2010 at 6:52
  • 1
    Well then, as david suggested in his answer, you'll either have to keep them as top level variables or create a closure. click doesn't know what you want want to pass into the handler. Commented Oct 22, 2010 at 6:55

4 Answers 4

126

see event.data

commentbtn.bind('click', { id: '12', name: 'Chuck Norris' }, function(event) {
    var data = event.data;
    alert(data.id);
    alert(data.name);
});

If your data is initialized before binding the event, then simply capture those variables in a closure.

// assuming id and name are defined in this scope
commentBtn.click(function() {
    alert(id), alert(name);
});
Sign up to request clarification or add additional context in comments.

1 Comment

The jQuery documentation for the .click() method indicates this ability but offers no example of how to use it. But, if you look up 'event.data' [api.jquery.com/event.data/], there you find an example. Also check out Event Object: api.jquery.com/category/events/event-object
19

From where would you get these values? If they're from the button itself, you could just do

commentbtn.click(function() {
   alert(this.id);
});

If they're a variable in the binding scope, you can access them from without

var id = 1;
commentbtn.click(function() {
   alert(id);
});

If they're a variable in the binding scope, that might change before the click is called, you'll need to create a new closure

for(var i = 0; i < 5; i++) {
   $('#button'+i).click((function(id) {
      return function() {
         alert(id);
      };
   }(i)));
}

1 Comment

I was confused by the case that the variable is changed when click is called. Thank you for this one!
0

An alternative for the bind() method.

Use the click() method, do something like this:

commentbtn.click({id: 10, name: "JoĂŁo"}, onClickCommentBtn);

function onClickCommentBtn(event)
{
  alert("Id=" + event.data.id + ", Name = " + event.data.name);
}

Or, if you prefer:

commentbtn.click({id: 10, name: "JoĂŁo"},  function (event) {
  alert("Id=" + event.data.id + ", Nome = " + event.data.name);
});

It will show an alert box with the following infos:

Id = 10, Name = JoĂŁo

Comments

-1
var someParam = xxxxxxx;

commentbtn.click(function(){

    alert(someParam );
});

3 Comments

I want to pass them within the event.
@Prashant: Can you give an example for that?
@Prashant - you can't just 'add' more params to your callback! jQuery won't know where to get the values from...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.