How can I get the id of the element that triggered the jQuery .change() function?
The function itself works properly, but I need a specific action for a selector with id="next".
$("select").change(function() {
[...snip....]
alert( $(this).attr('id') ); // <---- not working
}
Any ideas why the alert above isn’t working?
thisis the DOM element on which the event was hooked.this.idis its ID. No need to wrap it in a jQuery instance to get it, theidproperty reflects the attribute reliably on all browsers.Live example
You’re not doing this in your code sample, but if you were watching a container with several form elements, that would give you the ID of the container. If you want the ID of the element that triggered the event, you could get that from the
eventobject’stargetproperty:Live example
(jQuery ensures that the
changeevent bubbles, even on IE where it doesn’t natively.)