Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda