I want an input field which calls a javascript function, if a key is pressed. But I'm not able to pass the event as well as an element reference. I can either pass the event:
<input type="text" name="chmod" value="644" onkeypress="chmod(e)">
or pass the element reference:
<input type="text" name="chmod" value="644" onkeypress="chmod(this)">
If I try to pass both there occurs an error:
<input type="text" name="chmod" value="644" onkeypress="chmod(e, this)">
Uncaught ReferenceError: e is not defined
Is there any way to pass both, the event and a reference to the element?
Cheers, Marco
To pass an event object to a function in JavaScript, we can use the addEventListener method. const onClick = (ev) => { ev. preventDefault(); console.
Passing the event object of react as the second argument. If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function.
To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .
The parameter e that you are asking about is an Event object, and it represents the event being fired which caused your function to be executed. It doesnt really have to be e , you can name it anything you want just like all other function parameters.
I'd do the following:
<input type="text" name="chmod" value="644" onkeypress="chmod">
Then your js:
function chmod(e) {
var element = e.target;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With