14

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,021
  • 25
  • 75
  • 119
  • 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

31

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
  • 60,782
  • 19
  • 111
  • 112
3

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
  • 837,282
  • 173
  • 1,862
  • 1,933
2

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

<input type="text" onchange="myfunction(this.value)" />
Nick Craver
  • 610,884
  • 134
  • 1,288
  • 1,151