Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send multiple arguments to jQuery click function?

Currently I'm using something like:

$('.myclass').click(function(){  
    var msg = $(this).attr('id');  
    alert(msg)
});

And HTML:

< a href="#" class="myclass" id="101">Link</a>

If I need additional parameters how would I read them? Also is the current way I am using the proper way? Originally I was using hidden input fields so it was already a step up. :p

like image 225
Roger Avatar asked Oct 08 '09 23:10

Roger


People also ask

How do you pass parameters to click an event?

If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.

How do you pass multiple parameters in onClick function in Reactjs?

Using an inline arrow function. Following the example from above, a very simple way of passing multiple attributes to your handler, is to call the handler from within an inline arrow function. The code added on the onClick line on the <a> element defines an inline arrow function.

How do you pass two parameters on onChange?

To pass multiple parameters to onChange in React:Pass an arrow function to the onChange prop. The arrow function will get called with the event object. Call your handleChange function and pass it the event and the rest of the parameters.


2 Answers

jQuery Events

jQuery .click()

$('.myclass').bind("click", { Param1: "", Param2: 2 }, function(event){
    alert(event.data.Param2);
});
like image 136
ChaosPandion Avatar answered Oct 06 '22 22:10

ChaosPandion


you can use bind() and send additional data to the event handler as event.data

like image 31
Russ Cam Avatar answered Oct 06 '22 23:10

Russ Cam