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

113
Views
JavaScript Class Function not accessible by Event Listener

The toggleLid function defined in my class is not working when called in an event listener. The individual 'backpack' objects are defined elsewhere. The browser is not throwing any errors in the console. Any advice is appreciated!

function Backpack (
    name,
    lidOpen
    
) {
    this.name = name;
    this.lidOpen = lidOpen;

    this.toggleLid = (lidStatus) => {
        this.lidOpen = lidStatus;
    };
};

const backPackList = [bag1, bag2];

const content = backPackList.map((backpack) => {
    let backpackArticle = document.createElement("article");
    backpackArticle.classList.add("backpack");
    backpackArticle.setAttribute("id", backpack.id)
    backpackArticle.innerHTML = `
    <h1>${backpack.name}<h1>
    <h2>${backpack.lidOpen}<h1>
    <button class='lid-toggle'>Toggle Lid</button>
    `;

    const button = backpackArticle.querySelector(".lid-toggle")

    button.addEventListener("click", () => {
        backpack.toggleLid(!backpack.lidOpen)
    })

    return backpackArticle;
});
about 4 years ago · Juan Pablo Isaza
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!