I want to make an eventHandler that passes "this" object as parameter. I tried this
<select id="customer" onchange="custChange(this);">
it works fine and get the the dom object on which on change even was called.
But as per my understanding this should not work as first argument is expected as "event" (not the "this" object )in event handler method like below
<select id="customer" onchange="custChange(event);">
can we pass any argument(this or event) in eventHandler method provide their name is correct or first argument will always be considered as event object?
To pass multiple parameters to onChange in React:Pass an arrow function to the onChange prop. The arrow function will get called with the event object. Call your handleChange function and pass it the event and the rest of the parameters.
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed. Tip: This event is similar to the oninput event.
The onChange event handler is a prop that you can pass into JSX <input> elements. This prop is provided by React so that your application can listen to user input in real-time. When an onChange event occurs, the prop will call the function you passed as its parameter.
Event Handling With the Select ElementThe onChange attribute of <select> lets you dynamically change something on screen, without reloading the page. For example, your can use it to display a particular image, depending on the user's selection.
You defined custChange
and more importantly: you are calling it. Hence you can decide for yourself which arguments it should accept and in which order. Only for functions that are not called by you, you have to pay attention to the order of arguments.
But if you want to define custChange
so that it is compatible with other ways of binding event handlers, i.e.
function custChange(event) {
// `this` refers to the DOM element
}
then you can call it with
custChange.call(this, event);
I have made a test.
function change(){
console.info( arguments );
}
<select name="" id="" onchange="change( this, event )">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<select name="" id="" onchange="change( event, this )">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<select name="" id="" onchange="change( this, e )">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
I operated selects above one by one, then got console-information below:
So, from the results, I got the conclusion that we must pass the correct word in page calling, and could change their position.
The first argument will always be the event.
[edit]You can call your handler with whatever argument you want, the event
and this
objects being available when called. event
refers to the event object, and this
refers to the dom object firing the event.[/edit]
However, in the handler event.currentTarget
will link back to the object that fired the event:
<script>
function custChange(event) {
// event.currentTarget = select element
}
</script>
<select id="customer" onchange="custChange(event);">
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