Skip to content
Advertisement

addEventListener uses every click instead of button click

I wanted to write a code, that stores some information in my local storage when I click on a button. The function for the local storage is working fine but the event listener doesn’t. The click event is not referring to the button. It refers on every click.

JavaScript

Advertisement

Answer

Use function instead of call event on load like:

JavaScript
JavaScript

As you can see in the example below the function will call without click

JavaScript
JavaScript
Advertisement