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

181
Views
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 answers
Answer question

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 Report

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 Report
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!