'How can I addEventListener inside of a function?

I'm trying to addEventListener to an HTML element with id a2, inside of a function, but Chrome Dev Tools say it's an object pointer event.

I put

    var a2 = document.getElementById("a2");

inside the function and it also doesn't work.

I would appreciate any help with this problem.

  var a1 = document.getElementById("a1");
  a1.addEventListener("click", play);

  var a2 = document.getElementById("a2");

  function play () {
      if (this.id == a1) {
          this.removeEventListener("click", play);
          a2.addEventListener("click", play);
      }
      // do something
  };  


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source