Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing this object as argument in onChange event?

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?

like image 611
emilly Avatar asked Aug 08 '13 06:08

emilly


People also ask

How do you pass an argument to onChange?

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.

How do you get an event on onChange?

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.

What does onChange pass?

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.

Can I use onChange in select tag?

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.


3 Answers

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);
like image 171
Felix Kling Avatar answered Oct 27 '22 19:10

Felix Kling


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:

  • [select, Event]
  • [Event, select]
  • Uncaught ReferenceError: e is not defined

So, from the results, I got the conclusion that we must pass the correct word in page calling, and could change their position.

like image 24
Zhang Yang Avatar answered Oct 27 '22 21:10

Zhang Yang


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);">
like image 38
Thierry J. Avatar answered Oct 27 '22 19:10

Thierry J.