{"id":3200,"date":"2013-03-24T19:05:26","date_gmt":"2013-03-24T19:05:26","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3200"},"modified":"2013-03-24T19:05:26","modified_gmt":"2013-03-24T19:05:26","slug":"jquery-on-click-event-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-on-click-event-examples\/","title":{"rendered":"jQuery onClick Event Examples"},"content":{"rendered":"<p>Wondering how to use the <em><strong>jQuery\u00a0onclick event<\/strong><\/em>? Do you wish to execute some custom code on click of a button, a hyperlink, etc.? If yes, then this article is for you. In this article, I am going to share easy examples that show how to trigger an action when an element on a web page is clicked.<\/p>\n<p><!--more--><\/p>\n<h2>How to use the jQuery onClick event<\/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=3200\" 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 look at how to use the it by using means of examples.\u00a0Let&#8217;s assume that we have different elements in a web page such as button, image, hyperlink, div, etc. We will see different variations of the on click event.<\/p>\n<p><strong>Common HTML Source for all the following examples:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true crayon-selected\" title=\"Common HTML Source for all the following examples:\">&lt;style type=\"text\/css\"&gt;\n#my_div1  {\n\tbackground-color: #3C6;\n\tborder: 1px solid #090;\n}\n\n.custom_class {\n\tbackground-color: #FC9;\n\tborder: 1px solid #F90;\n}\t\n&lt;\/style&gt;\n\n\t&lt;div id=\"my_div1\"&gt;This is my existing Div 1.&lt;\/div&gt;\n\n    &lt;input type=\"submit\" name=\"sbt_show_hide_div\" id=\"sbt_show_hide_div\" value=\"Hide\/Show DIV on Button Click\"&gt;\n\n\t&lt;p id=\"para1\"&gt;This is a Paragraph 1.&lt;\/p&gt;\n\n\t&lt;input type=\"submit\" name=\"sbt_add_class2p\" id=\"sbt_add_class2p\" value=\"Add Class to PARAGRAPH  on Button Click\"&gt;\n\n&lt;ul&gt;\n    &lt;li&gt;List Item&lt;\/li&gt;\n    &lt;li&gt;List Item&lt;\/li&gt;    \n    &lt;\/ul&gt;\n\n&lt;input type=\"submit\" name=\"sbt_alert_li_items_count\" id=\"sbt_alert_li_items_count\" value=\"Alert the number of LIST ITEMS on Button Click\"&gt;\n\n&lt;p&gt;\n&lt;a href=\"http:\/\/www.theextremewebdesigns.com\" class=\"no_navigate\"&gt;Click me Go to theextremewebdesigns.com&lt;\/a&gt;\t&lt;\/p&gt;\n&lt;p&gt;\n  &lt;input type=\"submit\" name=\"sbt_alert_href\" id=\"sbt_alert_href\" value=\"Alert the href value of the above link on Click of Button\"&gt;    \n&lt;\/p&gt;\n\n&lt;p&gt;\n      &lt;label&gt;\n        &lt;input type=\"checkbox\" name=\"states\" value=\"Texas\" id=\"states_0\"&gt;\n  Texas&lt;\/label&gt;\n      &lt;br&gt;\n      &lt;label&gt;\n        &lt;input type=\"checkbox\" name=\"states\" value=\"Alabama\" id=\"states_1\"&gt;\n        Alabama&lt;\/label&gt;\n      &lt;br&gt;\n      &lt;label&gt;\n        &lt;input type=\"checkbox\" name=\"states\" value=\"Florida\" id=\"states_2\"&gt;\n        Florida&lt;\/label&gt;\n\n&lt;\/p&gt;\n&lt;p&gt;\n  &lt;input type=\"submit\" name=\"sbt_alert_checked_checkbox_val\" id=\"sbt_alert_checked_checkbox_val\" value=\"Alert Values of all Checked Checkboxes onClick of Button\"&gt;    \n&lt;\/p&gt;\n\n&lt;p&gt;\n  &lt;input type=\"submit\" name=\"sbt_reload_page\" id=\"sbt_reload_page\" value=\"Reload this page On Click of Button\"&gt;    \n&lt;\/p&gt;\n\n&lt;p&gt;\n  &lt;input type=\"submit\" name=\"sbt_onclick_redirect\" id=\"sbt_onclick_redirect\" value=\"Redirect to a URL when button is clicked\"&gt;    \n&lt;\/p&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function(){\n\n\/\/Place all your jQuery code here \n\n});\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 1:\u00a0Toggle div on button click event<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 1\">\/\/Toggle div when button is clicked \n$('#sbt_show_hide_div').on(\"click\", function(e){\n\n\t$(\"#my_div1\").toggle();\n\n\te.preventDefault();\n\n});<\/pre>\n<h3>Example 2:\u00a0Add class to p\u00a0on button click event<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 2\">\/\/Add class to p when button is clicked\n$('#sbt_add_class2p').on(\"click\", function(e){\t\t\t\n\n\t$(\"#para1\").addClass(\"custom_class\");\t\t\t\n\n\te.preventDefault();\t\t\n\n});<\/pre>\n<h3>Example 3: Alert the count of list items\u00a0on button click event<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 3\">\/\/Get the count of list items when button is clicked\n$('#sbt_alert_li_items_count').on(\"click\", function(e){\t\t\t\n\n\talert( $(\"ul li\").length );\n\n\te.preventDefault();\n\n});<\/pre>\n<h3>Example 4: Prevent hyperlink navigation even if the link is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 4\">\/\/Prevent hyperlink navigation even if the link is clicked\n$('.no_navigate').on(\"click\", function(e){\t\t\t\n\n\talert( 'Even though you clicked on a hyperlink, the navigation wont occur as we are using e.preventDefault() to stop it.' );\n\n\te.preventDefault();\n\n});<\/pre>\n<h3>Example 5: Alert the href value of a hyperlink\u00a0on hyperlink click event<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 5\">\/\/Get the href value of a hyperlink\n$('#sbt_alert_href').on(\"click\", function(e){\t\t\t\n\n\talert( $('.no_navigate').attr('href') );\n\n\te.preventDefault();\n\n});<\/pre>\n<h3>Example 6: Alert the values of all checked checkboxes\u00a0on button click event<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 6\">\/\/Get the values of all checked checkboxes\n$('#sbt_alert_checked_checkbox_val').on(\"click\", function(e){\t\t\t\n\n\t$('input[name=\"states\"]:checked').each(function(index) {\n\t    alert( $(this).val() );\n\t})\n\n\te.preventDefault();\n\n});<\/pre>\n<h3>Example 7: \u00a0Reload page when button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 7\">\/\/Reload page when button is clicked\n$('#sbt_reload_page').on(\"click\", function(e){\n\n\tlocation.reload();\n\n});<\/pre>\n<h3>Example 8:\u00a0Redirect to a different URL when button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 8\">\/\/Redirect to a different URL when button is clicked\n$('#sbt_onclick_redirect').on(\"click\", function(e){\n\n\twindow.location.href = \"http:\/\/www.theextremewebdesigns.com\";\n\n});<\/pre>\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=3200\" 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>Your Turn!<\/h5>\n<p>Do you know of any other ways to use the <em><strong>jQuery\u00a0<\/strong><strong><em>on click event<\/em><\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wondering how to use the jQuery\u00a0onclick event? Do you wish to execute some custom code on click of a button, a hyperlink, etc.? If yes, then this article is for you. In this article, I am going to share easy examples that show how to trigger an action when an element on a web page [&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":[42,43,41],"class_list":["post-3200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-demos-2","tag-jquery-snippets","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-PC","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3200","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=3200"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3200\/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=3200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}