Handle Event and define parameter after event parameter in javascript

Ariya Shiri
let testTime = document.querySelectorAll(".testTime"); // this List has 3 value. 30, 60, 120
testTime.forEach((x) => {
    console.log(x.innerHTML);
    addEventListener("click", selectTime(this, x));
});

function selectTime(e, item) {
    e.preventDefault();
    
}

I want to define a parameter, after e parameter and e.preventDefault() for the function selectTime.

And I want to use item in selectTime

testTime.forEach((x) => {
    console.log(x.innerHTML);
    addEventListener("click", selectTime(this, x));
});

I use this keyword but It gave me an error

Uncaught TypeError: e.preventDefault is not a function
    at selectTime (time.js:20:7)
    at time.js:13:31
    at NodeList.forEach (<anonymous>)
    at time.js:11:10
IP002

Your logic and syntax is all wrong. You don't callthe event listener to anything in your forEach loop and you are calling selectTime the wrong way in the addEventListener function. addEventListener must be always called on some DOM element, and as the second argument you pass it a callback function which will be called after the click is fired. When you pass a callback function, you just pass the function definition, without parentheses. What you're doing now is calling the function. This is how the code should be properly structured

let testTime = document.querySelectorAll(".testTime");
testTime.forEach((time) => {
    console.log(time.innerHTML);
    time.addEventListener("click", (event) => {
        selectTime(event, time)
    });
});

function selectTime(e, item) {
    e.preventDefault();
    
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript giving Event as parameter

How to passing the parameter to event handle in reactjs

How does the event parameter work in javascript?

Javascript - onClick event passing same parameter to function

how to define a global function that has an event parameter in lua

React handle change function works without parameter event

Pass a parameter to aasm event

passing parameter for event listener

Passing a parameter into an event handler

Parameter in a jquery event

event that adds a parameter to routing

Set event handler as parameter

Passing Event handler as parameter

jQuery sending event as parameter

Handling event method with parameter without inserting parameter

How to pass input text as parameter to javascript onblur event?

What exactly is the parameter e (event) and why pass it to JavaScript functions?

How to call page method with a parameter through in a javascript event?

Using "this" as parameter in an existing Javascript function called by jQuery.on() event

Javascript custom function with a callback parameter to be executed in a event listener with different parameters?

Passing both event and parameter to onchange bound function in JavaScript

Get calling Event inside of Class-Function without Parameter in JavaScript

how to unsubscribe an event with parameter(s)?

Button press event with parameter not fired

Passing an additional parameter with an onChange event

React onClick - pass event with parameter

Pass input parameter to .click event

Passing parameter to onchange event in Rails

pass parameter to click event of a class