Manually pressing the button () on the button, can I pass any parameters?

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

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

Is it possible?

+8
jquery
source share
4 answers

You need to call .trigger() . You can pass any number of arguments.

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

These additional parameters are then passed to the event handler:

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

Link: .trigger ()

+20
source share

Not. Your onclick event is independent of your click() function. This is usually possible with arguments[] , but in this case you will have to use a variable.

0
source share
 <button id='test'> var btn = $("button#test"); btn.attr("param", "my parameter"); btn.click(); 
0
source share

What argument do you want to pass? I understand that these arguments are static since clicking a button cannot give you any calculations.

When you catch a click, you can use this , so use it to see which arguments you want to pass

HTML:

 <input type="button" id="foo" /> 

And your JS:

 <script> function iNeedParams(bar, baz) { // Computation } $("id").click(function() { var me = $(this); // me contains the object // me.attr("id") gives you its ID, then you can call iNeedParams("hello", "world"); }); </script> 
0
source share

Source: https://habr.com/ru/post/651414/


All Articles