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

187
Visualizações
PreventDefault() with named function and function parameter

I'm trying to add an e.preventDefault() call to my event listener. My function call "voteNoClick" needs to pass a parameter ("direction"). How do I pass both the direction parameter and the event object into the function so I can then add e.preventDefault() within the function? When I try to add the event "e" as a parameter in addition to "direction" it seems to think e is an ordinary parameter.

Thanks in advance!

document.getElementById('voteButtonNoID').addEventListener('click', voteNoClick(direction));
function voteNoClick(direction) {
       // I want to add e.preventDefault() here
        document.getElementById("voteMessageID").innerHTML = "Waiting for the votes to be tallied...";
        voteResults('no',direction);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Hello you can try this

document.getElementById('voteButtonNoID').addEventListener('click', (e) => voteNoClick(e, direction));

function voteNoClick(e, direction) {
 e.preventDefault();
 document.getElementById("voteMessageID").innerHTML = "Waiting for the votes to be tallied...";
 voteResults('no',direction);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

return another function inside it and you can access event like this.

function voteNoClick(direction) {
    return function(event){
    // your code ...
        event.preventDefault()
        document.getElementById("voteMessageID").innerHTML = "Waiting for the votes to be tallied...";
        voteResults('no',direction);
    }
}
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