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);
CodePudding user response:
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);
}
CodePudding user response:
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);
}
}