Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The current element as its Event function param

Tags:

javascript

dom

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'))"
like image 474
Florian Müller Avatar asked Nov 24 '10 14:11

Florian Müller


3 Answers

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
}
like image 120
hunter Avatar answered Oct 14 '22 03:10

hunter


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.

like image 27
SLaks Avatar answered Oct 14 '22 03:10

SLaks


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

<input type="text" onchange="myfunction(this.value)" />
like image 28
Nick Craver Avatar answered Oct 14 '22 02:10

Nick Craver