16

I've got an element

<input type="text">

On this, there is an Event

onChange="myfunction(param)".

"param" is the content of the input itself. How can I handle, that, when I fire onChange (so complete the change of the field), in this param is the actual value of this field?

Is it possible to do something like that:

onChange="myfunction(document.getElementById('this_id'))"
Florian Müller
  • 7,448
  • 25
  • 78
  • 120
  • You can use `onchange="myfunction.call(this)"` as from here: https://stackoverflow.com/a/12812974/1720476 Where "this" will be pointing to current object, and you can get params then as you wish with `this.` heyword in a called function.. – Arnis Juraga Oct 08 '17 at 14:49

3 Answers3

33

You can pass this to myFunction which will be the input

<input type="text" onChange="myfunction(this)" />

then myFunction could look like this:

function myFunction(obj)
{
    var value = obj.value; // the value of the textbox
}
hunter
  • 62,308
  • 19
  • 113
  • 113
4

Inside an inline event handler, this will refer to the DOM element.

Therefore, you can write onchange="myfunction(this)" to pass the DOM element itself to the function.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

To get the .value inline, it would look like this:

<input type="text" onchange="myfunction(this.value)" />
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155