javascript: add an event listener to an inexistent element.
Tags: javascript, dom, eventlistener, html
Published: 2020-11-21 18:50:04

When writing the code for your website or app, manipulating the DOM ( document object model ) is a critical skill you must have, so in this article, we will see how to add an event listener to an inexisted element and also some important tips and tricks to avoid unexpected and weird bugs.
I will be using codepen to better demonstrate the example.
[**the first codepen example**](https://codepen.io/gugocharade/pen/NWroLBv?editors=1111&__cf_chl_jschl_tk__=f12c338e28c51b957f4f94c943d4ffc12f1687a9-1605977634-0-ARbChQoBqRQWglJ1XIseQWAZJZ4G66pwTjId7B7rPXGlt8nSroN16w1JvUwzz1liQo6zHdJEM6fDc5YMuAu9CW_SkmP_sUpctEBpitE05YjyTHhX1S5KXsYArr4fLfpjg5_BoS8qgFDiq62ZTdxqKLJTWQSxK4ipze4TMP7klWzeIjR2i71i3w5aLW7vB5dxK7_cirnK8KxF2F9hmOlR5f_hnZX8evRogJerXq6WH3kvzCIFe7V1-a6Hrr2fZ1QP91LK6eUITX-m8vFoTtb7KwPPLspGdh-bjBrXAIDJD2NDeRfbXbvmRZgqzdKxyIF3khBKrdklqUGCDRX93QEitZ74INMidDYrBvHvVZF8AH72Wji6GNgep_rxdxusJg_FWiS9ACfSDNjhp53bt-s2EEu01AVw-sVvxJ9LDmvcN_Bj)

## 1-Adding a click event to the future element:

The HTML I’m using in the first example is just a simple button that says **Go to login page**.

```html
<!DOCTYPE html>
<html>
<head>
<title>DOM manipulation</title>
</head>
<body>

<button class="login">Go to Login page</button>

</body>
</html>
```

For Javascript, I'm adding an event to the button listening for a click, so when the button is clicked, the event function will clear the DOM and insert a login form.

```js
const body = document.querySelector('body');
const btn = document.querySelector('.login');

btn.addEventListener('click', (e)=>{
  
  body.innerHTML = '';
  
  body.insertAdjacentHTML('afterbegin', `
  <form action="#" class="form">
  <input type="email" class="input" placeholder="Email">
  <input type="password" class="input" placeholder="Password">
  
  <button type="submit" class="submit-btn">Login</button>
  </form>
  `)
  
})
``` 

The future element I'm willing to attach the click event to is the button with class **submit-btn**, to do so we need to determine the closest and always existing parent (in this example is the body) and attach the click event to it, then we will test if the targeted element has the class **submit-btn**, if so, I'm going to prevent the form from submitting and just write to the console **you are logged in :)**.

```js
body.addEventListener('click', event => {
  if(event.target.matches(".submit-btn")){
    event.preventDefault()
    console.log("you are logged in :)")  
  }
})
```
##2-Adding a different event to the future element:

What if the event we want to attach is not a click let say it’s a *change* event, how we are going to implement it?
[**the second codepen example**](https://codepen.io/gugocharade/pen/vYKqjLp?editors=1111)

The HTML I'm using in this example is a button that says **Choose a car**.

```html
<!DOCTYPE html>
<html>
<head>
<title>DOM manipulation</title>
</head>
<body>

<button class="btn">Choose a car</button>

</body>
</html>
```

the javascript I will use in this example is the same but instead of inserting a form, I will insert a select element with cars names as options.

```js
btn.addEventListener('click', (e)=>{
  
  body.innerHTML = '';
  
  body.insertAdjacentHTML('afterbegin', `
  <label for="cars">Choose a car:</label>

<select name="cars" id="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
  `)
  
})
```

The future element we want to attach the change event to it is the select element, so we will attach a click event to the body (the closest and always existing parent), testing if the targeted element has the ID **cars** if so, we will add a change event to the select element, in the callback function for the change event I will store the select value in a car variable and write it to the console.

```js
body.addEventListener('click', event => {
  if(event.target.matches('#cars')){
    document.getElementById('cars').addEventListener('change', e => {
      const car = document.getElementById('cars').value;
      console.log(car);
    })
  }
})
```

It works, BUT WAIT A MINUTE.

At first, it looks like it's working, then when you do it again the event fires multiple times, why is that happening?

The first time I got this error I was about to get crazy, I searched everywhere, I tried all the solutions I found but nothing worked for me, then I started experimenting with my code, and BOOOM I found the solution.

the problem why this is happening is because each time we click the select element a change event gets attached to it because the callback function we are using is anonymous, so the solution is declaring the function with a name and then use it as a callback.

```js
const eventHandler = e => {
      const car = document.getElementById('cars').value;
      console.log(car);
    }

body.addEventListener('click', event => {
  if(event.target.matches('#cars')){
    document.getElementById('cars').addEventListener('change', eventHandler)
  }
})
```

I suggest experimenting with it in codepen example so you can get a better understanding of what happening. I won't fix it in the example, I want to give you the chance to fix it your self to understand it and memorize it.
thank you for reading this article, I hope you liked it and learned something from it. And please excuse my English.