Monday, July 20, 2020

How To Trigger A Button Click Event With Javascript

How To Trigger A Button Click Event With Javascript


Occasionally you will want to trigger an event on an element without directly causing it.
In this article I will explain how to manually trigger a click event for a button without having to click on it.
In fact, the click event can be replaced with any other event that you want to trigger, though the click event is probably the most common you will require.

Use the JQuery .trigger() method

In the below example I’ve created 2 buttons and registered a click event for each of them.
If you click button1, you will get an alert telling you so, however if you click button 2, you will execute the click event for button 2, but because we’ve used the JQuery .trigger() method, you will also execute the click event for button1.

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. <script src="Scripts/jquery-1.10.2.min.js"></script>
  6. <script type="text/javascript">
  7. $(document).ready(function () {
  8.  
  9. $("#button1").click(function () {
  10. alert("You clicked button 1");
  11. });
  12.  
  13. $("#button2").click(function () {
  14. alert("You clicked button 2");
  15.  
  16. //This will trigger the click event of button1
  17. $("#button1").trigger("click");
  18. });
  19.  
  20. });
  21. </script>
  22. </head>
  23. <body>
  24. <input id="button1" type="button" value="Button 1"/>
  25. <input id="button2" type="button" value="Button 2" />
  26. </body>
  27. </html>

Example Implementation:

The previous example of how to trigger a button click event might not make it obvious why you would do such a thing.

Here’s an example of a more realistic scenario.

Suppose you have a select HTML element with many options and 2 buttons, “Apply Filters” and “Get Top 5”.
When the “Apply Filters” button is clicked, all of the selected options will be sent to the server, however, if the user presses the “Get Top 10” button, rather than writing out your AJAX statement, validation logic etc again, you could select the top 10 items in the select element (as if the user had selected them themselves) and then trigger the click event on the “Apply Filters” button.

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. <script src="Scripts/jquery-1.10.2.min.js"></script>
  6. <script type="text/javascript">
  7. $(document).ready(function () {
  8.  
  9. $("#btnApplyFilters").click(function () {
  10. alert("You clicked button 1");
  11.  
  12. //AJAX here which posts back to the server
  13. var myData = {
  14. options: $("select option:selected").toArray()
  15. };
  16.  
  17. //This wont really work as "/someLocation" is not a real url
  18. $.ajax({
  19. url: "/someLocation",
  20. async: true,
  21. data: JSON.stringify(myData),
  22. contentType: 'application/json; charset=utf-8',
  23. dataType: "json",
  24. type: "POST",
  25. success: function (data) {
  26. alert("success " + data);
  27. },
  28. error: function (data) {
  29. alert("error " + data);
  30. },
  31. complete: function () {
  32. alert("complete");
  33. }
  34. });
  35. });
  36.  
  37.  
  38. $("#btnGetAll").click(function () {
  39. alert("You clicked button 2");
  40.  
  41. //select the top 5 options
  42. $("select option:lt(5)").attr("selected", "selected");
  43.  
  44. //This will trigger the click event of btnApplyFilters
  45. $("#btnApplyFilters").trigger("click");
  46. });
  47. });
  48. </script>
  49. </head>
  50. <body>
  51. <input id="btnApplyFilters" type="button" value="Apply Filters" />
  52. <input id="btnGetAll" type="button" value="Get All" />
  53. <select size="10" multiple>
  54. <option>option1</option>
  55. <option>option2</option>
  56. <option>option3</option>
  57. <option>option4</option>
  58. <option>option5</option>
  59. <option>option6</option>
  60. <option>option7</option>
  61. <option>option8</option>
  62. <option>option9</option>
  63. <option>option10</option>
  64. </select>
  65. </body>
  66. </html>
If you enjoyed this article, please consider giving it a +1


Online Course

React Native Chat App with Firebase - Firestore Course 2020


This course is for every programmer who wants to learn mobile application development and want to increase their skills to the next level.

Online Course


This is a Beginner to Advanced course and perfectly suitable for anyone interested to learn jQuery effects and animations for their web projects. If you have basic knowledge of HTML, CSS, and JavaScript then you're good to go. After finish, you'll learn 12 really cool effects that you can use in your web projects. Moreover, using this knowledge you'll be able to do more interesting effects on your own. 



No comments:

Post a Comment

The 3 Most Popular Front-end Frameworks Compared

  There’s an outsized number of front-end frameworks available today, each with different strengths and weaknesses. This makes it tricky to ...