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

182
Visualizações
buttons are like one, when i click either button they both do the same thing

why isn't it not working when I press the previous button, it's like both the previous and next button are the same

here's the code: https://codepen.io/pinkypink/pen/NWjzMre

const images = document.querySelector('.carousel_images');
const buttons = document.querySelectorAll('.carousel_button');

const imageCount = document.querySelectorAll('.carousel_images img').length;
let imageIndex = 1;
let translateX = 0;

buttons.forEach(button => {
  button.addEventListener('click', e => {
    if (e.target.id === 'next') {
      if (imageIndex !== 1) {
        imageIndex--;
        translateX += 650;
      }
    } else {
      if (imageIndex !== imageCount) {
        imageIndex++;
        translateX -= 683;
      }
    }
    
    images.style.transform = `translateX(${translateX}px)`;
  });
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use e.currentTarget not e.target.

e.target is the element that was clicked - in your case the SVG element.

e.currentTarget is the element whose event listener is being called - in your case the div.

about 4 years ago · Juan Pablo Isaza Relatório

0

What I am pointing out in the comment is that if you add the console... statement, you will see that e.target.id returns an empty string ("").

Therefore, the if statement becomes

if ("" === 'next') {

which will always be false so the else statement runs which is the previous button.

buttons.forEach(button => {
  button.addEventListener('click', e => {
    if ("" === 'next') {  <-------------------------------------
      if (imageIndex !== 1) {
        imageIndex--;
        translateX += 650;
      }
    } else {
      if (imageIndex !== imageCount) {
        imageIndex++;
        translateX -= 683;
      }
    }
    
    images.style.transform = `translateX(${translateX}px)`;
  });
});

This answers your question: why isn't it working when I press the previous button, it's like both the previous and next button are the same

about 4 years ago · Juan Pablo Isaza Relatório
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