user10614851
user10614851

Reputation:

Event listener triggers on extension icon click, not popup button

I'm developing a browser extension where the user can click on one of many buttons in the popup. I have my code stored in popup.js. The following code is supposed to execute only when you click the button with id="green". Instead, it executes whenever you click the extension's icon for the first time.

<!DOCTYPE html>
  

<html>
    <head>
      <style>
        button {
          height: 20px;
          width: 100px;
          outline: none;
        }
      </style>
      
    </head>
    <body>
      <button id="oolong">Oolong</button>
      <button id="green">Green</button>
      <button id="purerh">PurErh</button>
      <button id="Black">Black</button>
      <span id="audio"></span>
      <script src="popup.js"></script>
    </body>
  </html>

document.getElementById("green").addEventListener("click", alert("Test"))

What do I need to do to make it only execute on the click of the button with id='green'?

Upvotes: 1

Views: 363

Answers (1)

David Moreno Cortina
David Moreno Cortina

Reputation: 146

document.getElementById("green").addEventListener("click", function(){alert("Test")})

You have pass to addEventListener a function to execute. What you are doing is passing the return of a execution

Upvotes: 1

Related Questions