# 🛑 Why You Should Avoid the onclick Attribute in Your Code

Hey everyone! It's been a while since I wrote my last article. But here I am with another topic that might be informational to you 🙂.

This time I am writing about why you should avoid the `onclick` attribute in your HTML and JavaScript code.

![meme - you avoiding onclick](https://cdn.hashnode.com/res/hashnode/image/upload/v1670764630357/7W5GzIP-0.gif align="center")

> This article is not particularly about **click events** but rather all events in HTML and JS. The **click event** is a good and easy example to explain.

First, I will try to explain the various ways to handle click events or rather any events in JavaScript.

# Different Ways to Handle Click Events

There are three ways I know to handle click events in HTML and JavaScript (comment below if you know more).

## 1\. Using the `onclick` attribute

The first way I learned was by using the `onclick` attribute in HTML and calling a function that was written in JavaScript.

It is maybe the easiest way to handle click events. As there is nothing complicated. Everything makes sense.

```xml
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>The onclick way</title>
  </head>
  <body>
    <button onclick="whenButtonClicked()">Click me</button>
    <script>
      function whenButtonClicked() {
        alert('button clicked');
      }
    </script>
  </body>
</html>
```

In the code above, we create a function inside our JavaScript that we then call when the button is clicked.

It will result in something like this 👇

![button is clicked and an alert is shown](https://cdn.hashnode.com/res/hashnode/image/upload/v1670751015067/_4GdNviTz.gif align="center")

## 2\. Using the `addEventListener` function

The second way I learned was by using the `addEventListener` function inside JavaScript. I saw it for the first time in [this tutorial](https://youtu.be/jaVNP3nIAv0).

At first, I felt this was unnecessary as the `onclick` felt easier. But with time and building large projects, I understood why this makes more sense.

```xml
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>The onclick way</title>
  </head>
  <body>
    <button id="btn">Click me</button>
    <script>
      const btn = document.getElementById('btn');

      function whenButtonClicked() {
        alert('button clicked');
      }

      // 1. either pass the function
      btn.addEventListener('click', whenButtonClicked);
      // 2. or use anonymous function
      btn.addEventListener('click', () => {
        alert('button clicked');
      });
    </script>
  </body>
</html>
```

What happens here is you first **select the button** inside your JavaScript code with `id` or `class` etc. And then **register/add an event listener** called `click`. Then either **pass the function** in or use an **anonymous function**.

This is better because when you are going to build something large the first way, **your JavaScript code will get mixed with HTML.** Then maintaining your code will be very hard.

You can also remove an event listener using the `removeEventListener` method. By just passing in the function that was used previously.

```javascript
function whenButtonClicked() {
  alert('button clicked');
}

// add event listener
btn.addEventListener('click', whenButtonClicked);
// remove event listener
btn.removeEventListener('click', whenButtonClicked);
```

## 3\. Using the `onclick` property

This is by far my favourite way to handle events.

*   It's as straightforward as the first one.
    
*   Separated from HTML as the second one.
    

```xml
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>The onclick way</title>
  </head>
  <body>
    <button id="btn">Click me</button>
    <script>
      const btn = document.getElementById('btn');

      function whenButtonClicked() {
        alert('button clicked');
      }

      // 1. either pass the function
      btn.onclick = whenButtonClicked;
      // 2. or use anonymous function
      btn.onclick = () => {
        alert('button clicked');
      };
    </script>
  </body>
</html>
```

What we do in this piece of code is that we assign the `onclick` attribute to a function (same as the second way).

The downside of using this method is that you cannot assign multiple event listeners. If you use `addEventListener`. With that, you can add multiple functions to one element. And not to mention you also **cannot** use something like `removeEventListener`

# So What's Wrong with `onclick`?

Now let's come to the main section of the article.

The `onclick` attribute is not recommended in HTML because it is considered to be a **bad practice** to use **inline JavaScript** in your HTML code.

Instead, it is better to separate your JavaScript code from your HTML code and use event listeners to handle events in your web page. This makes your code easier to read, maintain, and reuse.

In general, it is better to avoid using the `onclick` attribute in HTML and to use event listeners instead, as this allows you to keep your JavaScript code separate from your HTML code and makes it easier to manage and maintain.

Additionally, using inline JavaScript in your HTML can make your code more difficult to maintain and update. If you need to change the behaviour of your web page, you will have to go through your HTML code and update all of the `onclick` attributes, which can be time-consuming and error-prone.

# **Conclusion**

I hope this article helped you out and made you better!

I would love to hear your thoughts on this topic. Please share your ideas and experiences in the comments section below.
