{"id":11318,"date":"2023-08-31T15:36:17","date_gmt":"2023-08-31T14:36:17","guid":{"rendered":"https:\/\/www.20i.com\/blog\/?p=11318"},"modified":"2024-03-06T08:39:08","modified_gmt":"2024-03-06T08:39:08","slug":"how-to-remove-onclick-events-with-javascript","status":"publish","type":"post","link":"https:\/\/www.20i.com\/blog\/how-to-remove-onclick-events-with-javascript\/","title":{"rendered":"How to remove OnClick events with Javascript"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">JavaScript is an essential tool for web developers, enabling the creation of interactive and dynamic website components. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of its key features is the ability to assign and manipulate onclick events, which trigger specific functions when a user clicks on a particular element on a web page.\u00a0\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, there might be some situations where you need to remove these events, either temporarily or permanently, to alter the behaviour of your web page.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this quick tutorial, we&#8217;ll guide you through the steps to remove onclick events in JavaScript, helping you gain more control over your web page&#8217;s interactivity.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are various ways to remove an onclick event handler using JavaScript, we will look at a couple of working examples, breaking down each snippet and you can even try it out yourself.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is some example HTML code to create a clickable button:\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button id=\"myButton\" onclick=\"alert('Hello world!')\"&gt;Click Me&lt;\/button&gt;&nbsp;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The HTML code above will create a button to alert the user with a message \u2018Hello World!\u2019. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Go to <a href=\"https:\/\/jsfiddle.net\/7e6zpx20\/1\/\">JSFiddle <\/a>to see it in action. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"144\" src=\"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2024\/03\/jsfiddle-Hello-world.png\" alt=\"\" class=\"wp-image-12206\" srcset=\"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2024\/03\/jsfiddle-Hello-world.png 624w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-Hello-world-300x69.png.webp 300w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-Hello-world-370x85.png.webp 370w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-Hello-world-270x62.png.webp 270w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-Hello-world-570x132.png.webp 570w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-Hello-world-150x35.png.webp 150w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">On JSFiddle, in the bottom right square, you will see a clickable HTML button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1399\" height=\"678\" src=\"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2024\/03\/jsfiddle-click-me-button.png\" alt=\"\" class=\"wp-image-12207\" srcset=\"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2024\/03\/jsfiddle-click-me-button.png 1399w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-click-me-button-300x145.png.webp 300w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-click-me-button-768x372.png.webp 768w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-click-me-button-370x179.png.webp 370w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-click-me-button-270x131.png.webp 270w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-click-me-button-570x276.png.webp 570w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-click-me-button-740x359.png.webp 740w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/jsfiddle-click-me-button-150x73.png.webp 150w\" sizes=\"auto, (max-width: 1399px) 100vw, 1399px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Click it and you&#8217;ll be shown this message:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"521\" height=\"206\" src=\"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2024\/03\/JSFiddle-Hello-world-message.png\" alt=\"\" class=\"wp-image-12208\" srcset=\"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2024\/03\/JSFiddle-Hello-world-message.png 521w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/JSFiddle-Hello-world-message-300x119.png.webp 300w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/JSFiddle-Hello-world-message-370x146.png.webp 370w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/JSFiddle-Hello-world-message-270x107.png.webp 270w, https:\/\/www.20i.com\/blog\/wp-content\/smush-webp\/2024\/03\/JSFiddle-Hello-world-message-150x59.png.webp 150w\" sizes=\"auto, (max-width: 521px) 100vw, 521px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now we are going to look at ways to prevent this functionality from working.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following JavaScript will retrieve the HTML button using the ID \u2018mybutton\u2019 and then remove the click functionality.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var button = document.getElementById(\"myButton\");&nbsp;\nbutton.onclick = null;&nbsp;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By setting button.onclick to null, we remove the onclick event handler from the button. When the event handler is null, clicking the button will not trigger any action.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Give it a try <a href=\"https:\/\/jsfiddle.net\/bdj6La2w\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. If you try clicking the button now, the above message will not display.\u00a0\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is another JavaScript example, this time we\u2019re using removeAttribute.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var button = document.getElementById(\"myButton\");&nbsp;&nbsp;\nbutton.removeAttribute(\"onclick\");&nbsp;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The code retrieves the button element with the ID \u2018myButton\u2019 and then removes the \u2018onclick\u2019 attribute from it which results in the button being unclickable.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Give it a try <a href=\"https:\/\/jsfiddle.net\/a3bq8hej\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>&nbsp;&nbsp;<\/p>\n\n\n<div class='code-block code-block-3' style='margin: 8px 0; clear: both;'>\n<hr>\n<br \/>\n<H4>Upgrade your hosting \ud83d\udcc8 Unleash your websites \ud83d\ude80<\/h4>\n<p>Build, deploy & manage all your sites\/apps at scale. Use our high-spec cloud servers to ensure blazing-fast load times, every time. Get market-leading speed, security & customer support.<\/p>\n<ul>\n<li>WordPress, WooCommerce, Laravel optimisations &amp; more<\/li>\n<li>One click migration from any host, any time<\/li>\n<li>Free Email, DNS, CDN, SSL, SSH, Backups, Security &amp; Git integration all baked-in<\/li>\n<li>Global reach with 60+ global data centres<\/li>\n<li>Award-winning support from real people<\/li>\n<\/ul>\n<p>Find out why over 1 million agencies, online stores, developers, multi-site hosting and high traffic sites use our <a href=\"https:\/\/www.20i.com\/managed-cloud-servers\">Managed Cloud Servers<\/a> to ensure peak performance, every time.<\/p>\n<br \/><br \/>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"JavaScript is an essential tool for web developers, enabling the creation of interactive and dynamic website components. One&hellip;","protected":false},"author":25,"featured_media":11320,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"ub_ctt_via":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","footnotes":""},"categories":[60],"tags":[79],"class_list":["post-11318","post","type-post","status-publish","format-standard","has-post-thumbnail","category-web-design-dev","tag-web-development","cs-entry"],"featured_image_src":"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2023\/08\/Javascript.png","author_info":{"display_name":"Ben Perry","author_link":"https:\/\/www.20i.com\/blog\/author\/benperry92\/"},"_links":{"self":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts\/11318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/comments?post=11318"}],"version-history":[{"count":6,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts\/11318\/revisions"}],"predecessor-version":[{"id":12209,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts\/11318\/revisions\/12209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/media\/11320"}],"wp:attachment":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/media?parent=11318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/categories?post=11318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/tags?post=11318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}