How to use simulated click button action with jQuery or javascript?

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

+44
javascript jquery
Feb 16 '11 at 10:08
source share
6 answers

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

 $('#foo').trigger('click'); 
+67
Feb 16 '11 at 10:10
source share

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

 $("#mybutton").click(); 
+17
Feb 16 '11 at 10:10
source share

Simple . click () :

 $("#theButton").click(); 
+11
Feb 16 '11 at 10:11
source share

Please try entering the following code, for example:

 setTimeout(function() { $(".class_name").trigger('click'); }, 8000); 
0
Sep 07 '16 at 10:12
source share

Having wrapped it in this “anonymous document function”, make sure that the button is clicked. Just in case, it tries to click before the item you want to click is loaded. This will fix this problem.

Also, remember that the identifier uses a hash (i.e. # close the button), but if you want to click on an element that has a class, replace the hash with one point (i.e. close button).

 jQuery(document).ready(function() { $("#close-button").trigger('click'); }); 
0
Oct 30 '16 at 10:04
source share

The following code will double-click to print one of $('.submitBtn').click(); and other $('.submitBtn').trigger('click')

 $(document).ready(function() { $('.submitBtn').on('click', function() { console.log("Clicked!"); }) $('.submitBtn').click(); $('.submitBtn').trigger('click') }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <button menu="submitBtn" class="submitBtn len4 btn" title="submit" data-code-m="sbm.submit"> Click me </button> 
0
Nov 05 '17 at 2:22 on
source share



All Articles