Consider this example of registering onchange callbacks:
function appendToSaveArray(field)
{
saveArray[field.attr("id")] = field.attr("value");
}
$('#fields input').change(appendToSaveArray);
The idea is storing the last change made to several text inputs in an array.
When I change the value of one of the inputs (and then switch the focus to another element of the document), the function appendToSaveArray is called.
However, the argument field does not contain the input element where the change was triggered, which I would have expected.
How can I find out from within the function appendToSaveArray, what input triggered the event?
Inside the
changeevent handler,thisrefers to the input for whom the event was triggered. You can get its value directly withthis.value:DEMO.