Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

manually calling click() on a button, can I pass any parameters?

Tags:

jquery

I am manually calling .click() on a button on a page in my jquery/javascript code.

I need to pass a parameter to click that I can then read on the function that responds to the click event.

is this possible?

like image 603
Blankman Avatar asked Jan 23 '11 19:01

Blankman


People also ask

How do you pass a value in Click event?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...

What is trigger() in jQuery?

jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.


1 Answers

You need to invoke .trigger(). You can pass over any amount of arguments there.

$('element').trigger('click', [arg1, arg2, ...]);

These extra parameters are then passed into the event handler:

$('element').bind('click', function(event, arg1, arg2, ...) {
}); 

Reference: .trigger()

like image 189
jAndy Avatar answered Nov 15 '22 20:11

jAndy