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

120
Views
How to access constructor variable from the method of event listener
class Carousel {
    constructor(el, imgSrc) {
        this.createCarousel(el, imgSrc);

        let max = imgSrc.length;
        this.max = max;
    }

    createCarousel(el, imgSrc) {
        const track = document.createElement("ul");
        track.classList.add("carousel__track");
        el.appendChild(track);

        for (let i = 0; i < imgSrc.length; i++) {
            const imageContainer = document.createElement("li");

            imageContainer.classList.add("carousel__image-container");
            const photo = document.createElement("img");
            photo.classList.add("carousel__image");
            photo.src = imgSrc[i];

            imageContainer.appendChild(photo);
            track.appendChild(imageContainer);
            this.imageContainer = imageContainer;
        }

        //next button
        const nextButton = document.createElement("button");
        nextButton.classList.add("carousel__next");
        nextButton.addEventListener("click", this.next);
        el.appendChild(nextButton);

        
    }
    next() {
        if (true) {
            
            console.log(this.max);
            
        }
    }
    
}

createCarousel(el, imgSrc) {
        const track = document.createElement("ul");
        track.classList.add("carousel__track");
        el.appendChild(track);

        for (let i = 0; i < imgSrc.length; i++) {
            const imageContainer = document.createElement("li");

            imageContainer.classList.add("carousel__image-container");
            const photo = document.createElement("img");
            photo.classList.add("carousel__image");
            photo.src = imgSrc[i];

            imageContainer.appendChild(photo);
            track.appendChild(imageContainer);
            this.imageContainer = imageContainer;           }

        //next button
        const nextButton = document.createElement("button");
        nextButton.classList.add("carousel__next");
        nextButton.addEventListener("click", this.next);
        el.appendChild(nextButton);   
        
    }
    next() {
        if (true) {
            console.log(this.max);
            
        }
    }
    
}

I need to access max variable from constructor in the method of click event listener (next). this will reference to button itself. How do I access the constructor variables. also need to access the imageContainer variable(one where the elements were created) in the next method,and be abble to add/mofify styles to it

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!