{"id":1162,"date":"2012-09-13T09:38:18","date_gmt":"2012-09-13T09:38:18","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=1162"},"modified":"2012-09-13T09:38:18","modified_gmt":"2012-09-13T09:38:18","slug":"jquery-trigger-click-trigger-method-binded-to-click-event-example","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-trigger-click-trigger-method-binded-to-click-event-example\/","title":{"rendered":"jQuery trigger click Example (Trigger method binded to Click event)"},"content":{"rendered":"<p>Let&#8217;s assume that you have set of elements in a web page, specifically form elements. Do you know how to <strong>trigger click of an element without actually clicking it<\/strong>? Have you ever wondered how you can bind the &#8220;click&#8221; event of an element to itself and to other multiple elements on the same page? In this article, I am going to share with you a very simple yet powerful way to use <em><strong>jQuery to trigger click event using an example<\/strong><\/em>. Read on to find out more.<!--more--><\/p>\n<h2>Example to Trigger Click by binding method to Click event using jQuery<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1162\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s assume that we have two buttons in our web page. The first button is called &#8220;Click 1&#8221; &amp; the second button is called &#8220;Click 2&#8221;. So let me show you, how to use the trigger method first. So here is the full source code for a basic trigger method:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;jQuery example for using trigger method binded to click event&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery-1.8.2.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;input type=\"submit\" name=\"sbt_click1\" id=\"sbt_click1\" value=\"Click 1\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_click2\" id=\"sbt_click2\" value=\"Click 2\"&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$('#sbt_click1').click(function() {\n\nalert( 'You clicked button: '+ $(this).val() );\n\n});\n\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>Simple so far? Just run the above code and click on the &#8220;Click 1&#8221; button. You will see the alert &#8220;You clicked button: Click 1&#8221;.<\/p>\n<p>Now to the most interesting part. So what essentially we would like to do next is to click on the &#8220;Click 2&#8221; button &amp; yet, show the same alert as we have shown for the &#8220;Click 1&#8221; button. Yes, of course, we can copy and paste the same code in the trigger method for &#8220;Click 2&#8221; button. But that&#8217;s just mere copy and paste. That doesn&#8217;t teach us how to actually use the <strong>trigger method and click events<\/strong> as effectively as they are meant to be. Do they?<\/p>\n<p>So what do we do now? Well, it&#8217;s simple. We will just go ahead and write a single line code in the &#8220;click&#8221; event of the &#8220;Click 2&#8221; button so that it &#8220;triggers&#8221; the\u00a0&#8220;click&#8221; event of the &#8220;Click 1&#8221; button automatically, without requiring us to copy &amp; paste any part of the code that we have written for &#8220;Click 1&#8221; button. Makes sense? Take a look at the source code and you will possibly understand it, if you haven&#8217;t so far. Here is the full source code to <em><strong>bind trigger method to click event in jQuery<\/strong><\/em>:<\/p>\n<pre class=\"lang:xhtml decode:true crayon-selected\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;jQuery trigger click example&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery-1.8.2.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;input type=\"submit\" name=\"sbt_click1\" id=\"sbt_click1\" value=\"Click 1\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_click2\" id=\"sbt_click2\" value=\"Click 2\"&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$('#sbt_click1').click(function() {\n\nalert( 'You clicked button: '+ $(this).val() );\n\n});\n\n\/\/This is where the \"magic\" happens\n\n$('#sbt_click2').click(function() {\n\n$(\"#sbt_click1\").trigger('click');\n\n});\n\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>So if you take a look at the code of the &#8220;Click 2&#8221; button, you will notice that we haven&#8217;t written any alerts in it. We haven&#8217;t copied any code from the click event of the &#8220;Click 1&#8221; button. Instead, we simply <strong>binded trigger method to the click event<\/strong> of the &#8220;Click 1&#8221; button. Makes sense now? Hope it does.<\/p>\n<p>This is a very simple example for <strong>jQuery trigger click<\/strong>. You can apply the same strategy to achieve different effects.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1162\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>Simple, isn&#8217;t it?<\/h5>\n<p>Do you know of any other ways to <strong>trigger click \u00a0of a button\u00a0using jQuery<\/strong>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s assume that you have set of elements in a web page, specifically form elements. Do you know how to trigger click of an element without actually clicking it? Have you ever wondered how you can bind the &#8220;click&#8221; event of an element to itself and to other multiple elements on the same page? In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[40,42,41],"class_list":["post-1162","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-2","tag-jquery-demos-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-iK","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=1162"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1162\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=1162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=1162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=1162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}