Using the named function and function parameter to call `preventDefault()` and avoid the default

I want to incorporate an e.preventDefault() call in my event listener. My function, "voteNoClick", needs to receive a parameter named "direction". How can I pass both the direction parameter and the event object into the function, allowing me to include e.preventDefault() within it? Whenever I try to add the event "e" as a parameter alongside "direction", it appears that e is perceived as a regular parameter.

I appreciate any help!

document.getElementById('voteButtonNoID').addEventListener('click', voteNoClick(direction));
function voteNoClick(direction) {
       // Here is where I intend to insert e.preventDefault()
        document.getElementById("voteMessageID").innerHTML = "Waiting for the votes to be tallied...";
        voteResults('no',direction);

Answer №1

Hey there, give this a shot

document.getElementById('voteButtonNoID').addEventListener('click', (e) => castNoVote(e, dir));

function castNoVote(e, dir) {
 e.preventDefault();
 document.getElementById("voteMessageID").innerHTML = "Awaiting the final vote count...";
 tallyVotes('no', dir);
}

Answer №2

Inside this function, you can include another function that allows for access to the event.

function clickNoVote(direction) {
    return function(event){
    // Add your code here ...
        event.preventDefault()
        document.getElementById("voteMessageID").innerHTML = "Please wait while the votes are counted...";
        tabulateVotes('no', direction);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular: Cleaning an image tag in sanitized HTML text

I have a scenario where I am integrating an HTML snippet from a trusted external source into my Angular component. To ensure security, I am utilizing Angular's DomSanitizer and specifically the bypassSecurityTrustHtml method to process the snippet bef ...

"Experience the latest version of DreamFactory - 2.0.4

I'm encountering a 404 error when I send a request to my new DSP. GET http://example.com/api/v2/sericename/_table/tablename 404 (Not Found) Upon checking the Apache error.log, I found this message: ... Got error: PHP message: REST Exception #404 &g ...

Transmitting JSON data containing nested objects through AngularJS

Looking to modify the way I am sending a POST request with nested child objects. Here is the current format: { "title": "sample string 2", "comment": "sample string 5", "child": { "name": "sample string 2" }, "children": [ { ...

Generating an image in Fabric.js on a Node server following the retrieval of canvas data from a JSON

I've been trying to solve this problem for the past few days. In my app, users can upload two images, with one overlaying the other. Once they position the images and click a button, the canvas is converted to JSON format and sent to a node (express) ...

Comparing the positions of elements in Selenium WebDriver with PHP

One of the elements on my webpage serves as a button that reveals a drop-down menu. Due to various factors affecting the page layout, there were some alignment issues between the button and the menu until a few bugs were fixed. I am now looking for a way t ...

Is the RouterModule exclusively necessary for route declarations?

The Angular Material Documentation center's component-category-list imports the RouterModule, yet it does not define any routes or reexport the RouterModule. Is there a necessity for importing the RouterModule in this scenario? ...

Obtain the popup URL following a fresh request using JavaScript with Playwright

I'm having trouble with a button on my page that opens a popup in a new tab. I have set up a listener to capture the URL of the popup when it opens: page.on('popup', async popup => { console.log('popup => ' + await pop ...

A guide to creating an HTTPS request using node.js

I am currently in the process of developing a web crawler. Previously, I utilized the following code for HTTP requests: var http=require('http'); var options={ host:'http://www.example.com', path:'/foo/example' }; ...

Adjusting package.json settings for an npm module

An npm package that I am using has an incorrect file path specified in its package.json. The current configuration is: "main": "./htmldiff.js", However, for it to function correctly, it should be: "main": "./src/html ...

Guide on transferring JSON information from a client to a node.js server

Below is the code snippet from server.js var express = require("express"), http = require("http"), mongoose = require( "mongoose" ), app = express(); app.use(express.static(__dirname + "/client")); app.use(express.urlencoded()); mongoose.con ...

Having difficulty implementing dynamic contentEditable for inline editing in Angular 2+

Here I am facing an issue. Below is my JSON data: data = [{ 'id':1,'name': 'mr.x', },{ 'id':2,'name': 'mr.y', },{ 'id':3,'name': 'mr.z', },{ & ...

What is the best way to activate a component within Angular 2 that triggers the display of another component through method invocation?

I have created a popup component that can be shown and hidden by calling specific methods that manipulate the back and front variables associated with the class. hide() { this.back = this.back.replace(/[ ]?shown/, ""); this.front = this.front.replace( ...

How can I tally the frequency of characters in a given string using Javascript and output them as numerical values?

I am in the process of tallying the frequency of each individual character within a given string and representing them as numbers. For example, let's consider the string "HelloWorld". HELLOWORLD There is one H - so 1 should be displayed with H remov ...

Using the JavaScript JSX syntax, apply the map function to the result of a

My aim is to create a structure resembling the following: <td> {phones.map((phone, j) => <p>{this.renderPhone(phone)}</p> )} </td> However, there may be instances where the phones array is not defined. Is it feas ...

Leverage webpack to consolidate multiple ES6 classes into a single file for easy importing via a script tag

For the past three days, I've been grappling with webpack in an attempt to complete a simple task that could have easily been done manually. However, I am determined to learn webpack for scalability reasons... I come to you now with a desperate quest ...

Deciphering a JSON response obtained from a jQuery output within a PHP context

I have created a simple search form that uses jQuery to query an external server for results. $("#formsearch").on("submit", function (event) { // all good! event.preventDefault(); submitFormSearch(); }); function submitFormSearch() ...

Altering the dimensions of a <div> based on the retrieved data

I am currently retrieving data from an API and displaying certain properties using mapping. I would like to adjust the width of the component based on the value of these properties. <h5> <span className="viewcount" ref={boxSize}> ...

The format of the date cannot be modified when using DesktopDatePicker as a React Component

I've been attempting to modify the appearance of the component, but the UI keeps showing the date in month-year format. <DesktopDatePicker inputVariant="outlined" label="Pick-up date" id="date ...

Updating databases with the click of a checkbox

Currently, I am developing a program for monitoring cars as part of my thesis. My current focus is on user management, and I have come across an issue where the database needs to be updated when the status of a checkbox changes. To visualize checkboxes, y ...

Automatically press the button when the display style is set to block

Hello everyone, I am completely inexperienced with this and I had a question - is there a way to automatically click a button when using display:block in styling? I would greatly appreciate it if someone could guide me in the right direction. <div i ...