Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use simulate the action of clicking button with jQuery or JavaScript?

I need to use JavaScript to simulate the action of clicking buttons. How do I achieve it? Can I achieve it with jQuery?

like image 208
Ricky Avatar asked Feb 16 '11 10:02

Ricky


People also ask

Can you click a button with Javascript?

Javascript has a built-in click() function that can perform a click in code.

How do you trigger a click button?

and a trigger to execute the button1 click event handler. $("#button2"). bind("click", (function () { alert("Button 2 is clicked!"); $("#button1").

How do you click a button in JQ?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.

How do you click in Javascript?

Method 1: Using the click() method: The click() method is used to simulate a mouse click on an element. It fires the click event of the element on which it is called. The event bubbles up to elements higher in the document tree and fires their click events also.


2 Answers

Yes, you can do it with jquery. Use trigger function. Here documentation. Here is sample:

$('#foo').trigger('click'); 
like image 76
gor Avatar answered Oct 20 '22 03:10

gor


You can execute the click event handler assigned to a button control:

$("#mybutton").click(); 
like image 27
jevakallio Avatar answered Oct 20 '22 03:10

jevakallio