Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
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
  };  
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Removed "a2" argument from play. Same code. JSFiddle

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

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

  function play() {
  
    console.log("a2")
    
    if (this.id == "a1") {
        console.log("a1")
      this.removeEventListener("click", play);
      a2.addEventListener("click", play);
    }
    // do something
  };
  
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!