{"id":17103,"date":"2025-10-16T09:44:03","date_gmt":"2025-10-16T08:44:03","guid":{"rendered":"https:\/\/www.20i.com\/blog\/?p=17103"},"modified":"2025-10-16T09:44:04","modified_gmt":"2025-10-16T08:44:04","slug":"the-difference-between-click-and-onclick-in-javascript","status":"publish","type":"post","link":"https:\/\/www.20i.com\/blog\/the-difference-between-click-and-onclick-in-javascript\/","title":{"rendered":"The difference between click and onclick in Javascript"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In JavaScript, click and onclick are two distinct approaches to handling click events. While they might seem similar at first glance, they differ in usage and behaviour. In this blog post, we\u2019ll dive into the practical differences and when to use each.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is onclick?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">onclick is a property of an element that you can assign just one function to. In our previous post, <a href=\"https:\/\/www.20i.com\/blog\/how-to-remove-onclick-events-with-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to remove onclick events with Javascript<\/a>&nbsp; we explored an example of an onclick event defined directly within HTML:&nbsp;<\/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; <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s a break down of what\u2019s happening:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>&lt;button&gt;<\/code><\/strong>: This is a standard HTML button element.&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>id=&#8221;myButton&#8221;<\/strong>: Gives the button a unique identifier so it can be targeted via JavaScript or CSS.&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>onclick=&#8221;alert(&#8216;Hello world!&#8217;)&#8221;<\/strong>:This tells the browser to run the alert(&#8216;Hello world!&#8217;) function when t<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So, when clicking the button, the pop-up alert will display the message \u201cHello World!\u201d. Give it a try <a href=\"https:\/\/jsfiddle.net\/7e6zpx20\/1\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is click?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">click is used with addEventListener&nbsp; and you can attach multiple functions to it without overwriting the previous function.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the same example code from above, but using <code>click<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button id=\"myButton\"&gt;Click Me&lt;\/button&gt; <\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This creates a button on the page with the text <strong>&#8220;Click Me&#8221;<\/strong>.&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It has an id of &#8220;myButton&#8221;, which allows JavaScript to find and interact with it.&nbsp;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt; \n\n  document.getElementById('myButton').addEventListener('click', function () { \n\n    alert('Hello world!'); \n\n  }); \n\n&lt;\/script&gt; <\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>document.getElementById(&#8216;myButton&#8217;)<\/strong>: Finds the button using its id.&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>.addEventListener(&#8216;click&#8217;, )<\/strong><strong>:<\/strong> Listens for a <strong>click event<\/strong> on that button.&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>function () { alert(&#8216;Hello world!&#8217;); }<\/strong>: This function runs when the button is clicked. It shows an alert box with the message <strong>&#8220;Hello world!&#8221;<\/strong>.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This approach separates your HTML from your JavaScript. Give it a try <a href=\"https:\/\/jsfiddle.net\/xkw1q469\/1\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Multiple Functions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Both click and onclick respond to user clicks, but the most practical difference is that onclick only allows one function at a time as setting a new one replaces the previous one.&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whereas click,with addeventlistener,lets you attach multiple functions to the same element.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/jsfiddle.net\/mecdans3\/\" target=\"_blank\" rel=\"noreferrer noopener\">Here<\/a> is an example of using two functions with click using the same coding above but with the added function \u2018console.log\u2019:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  button.addEventListener('click', function () { \n\n    console.log('Button was clicked!'); \n\n  }); <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will write to the console log \u2018Button was clicked!\u2019 as well as a pop-up box message \u2018Hello World.\u2019&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we try the same with onclick we will just get the console message. This is because of the limitation of onclick where you can only assign one function and adding more functions will overwrite the previous one. Give it a try <a href=\"https:\/\/jsfiddle.net\/qo7skgpw\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other differences<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Other than how click and onclick handle multiple functions, they also differ to how they\u2019re used in HTML and JavaScript.&nbsp;&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">HTML usage<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">onclick can be used directly in HTML. This works because onclick is an official HTML attribute mapped to the element&#8217;s JavaScript onclick property.&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">click cannot be used directly into HTML and would need to be coded in JavaScript in&nbsp;&nbsp;order to work as the browser wouldn\u2019t recognise click as an attribute.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, this wouldn\u2019t work:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>html \n\n&lt;button click=\"alert('Hello!')\"&gt;Click Me&lt;\/button&gt; <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;You can give it a try <a href=\"https:\/\/jsfiddle.net\/6zLrdy2u\/1\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">JavaScript usage<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Both click and onclick can be used in JavaScript.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">onclick is used by telling a specific HTML element what to do when it\u2019s clicked.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">click is used with addEventListener, allowing you to attach multiple functions to the same element without overwriting.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">onclick works for simple cases, for demos and quick tasks. It can be put directly into HTML, which makes it fast for testing.&nbsp; However, this approach can be limiting in larger projects and can lead to messy and hard to maintain code.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">click with addeventlistener , on the other hand, is a cleaner and more flexible approach, as it keeps JavaScript and HTML separated and allows to add more functions to the same event without conflict.&nbsp;&nbsp;<\/p>\n\n\n<div class='code-block code-block-2' style='margin: 8px 0; clear: both;'>\n\n<div class='ai-rotate ai-unprocessed ai-rotate-2-13508182' data-shares='WzUwLDEwMF0=' style='position: relative;'>\n<div class=\"ai-rotate-option\" data-index=\"1\" data-name=\"UmVzZWxsZXIgSG9zdGluZw==\" data-code=\"Cjxocj4KPGJyIC8+CjxhIGhyZWY9Imh0dHBzOi8vd3d3LjIwaS5jb20vcmVzZWxsZXItaG9zdGluZyIgdGFyZ2V0PSJfYmxhbmsiPjxpbWcgc3JjPSJodHRwczovL3d3dy4yMGkuY29tL2Jsb2cvd3AtY29udGVudC91cGxvYWRzLzIwMjYvMDMvQmxvZy1BZC1SZXNlbGxlci0xMjAweDYyNS0xLnBuZyIgbG9hZGluZz0ibGF6eSIgYWx0PSJVbmxpbWl0ZWQgUmVzZWxsZXIgSG9zdGluZyI+PC9hPgoK\">\n<\/div>\n<div class=\"ai-rotate-option\" data-index=\"2\" data-name=\"MjBpIFlvdVR1YmU=\" data-code=\"Cjxocj4KPGJyIC8+CjxhIGhyZWY9Imh0dHBzOi8vd3d3LnlvdXR1YmUuY29tL0AyMGlob3N0aW5nP3N1Yl9jb25maXJtYXRpb249MSIgdGFyZ2V0PSJfYmxhbmsiPjxpbWcgc3JjPSJodHRwczovL3d3dy4yMGkuY29tL2Jsb2cvd3AtY29udGVudC91cGxvYWRzLzIwMjYvMDUvMjBpLVlvdVR1YmUtMTIwMHg2MjUtMS5wbmciIGxvYWRpbmc9ImxhenkiIGFsdD0iMjBpIFlvdVR1YmUiPjwvYT4=\">\n<\/div>\n<\/div>\n<script>if (typeof ai_js_code == 'boolean') {var ai_block_div = document.querySelector ('.ai-rotate-2-13508182'); ai_process_rotation (ai_block_div); ai_block_div.classList.remove ('ai-rotate-2-13508182');};<\/script>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"In JavaScript, click and onclick are two distinct approaches to handling click events. While they might seem similar&hellip;","protected":false},"author":25,"featured_media":17105,"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":[51,60,47],"tags":[],"class_list":["post-17103","post","type-post","status-publish","format-standard","has-post-thumbnail","category-technology","category-web-design-dev","category-web-hosting","cs-entry"],"featured_image_src":"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2025\/10\/no-title-4.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\/17103","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=17103"}],"version-history":[{"count":4,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts\/17103\/revisions"}],"predecessor-version":[{"id":17113,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts\/17103\/revisions\/17113"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/media\/17105"}],"wp:attachment":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/media?parent=17103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/categories?post=17103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/tags?post=17103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}