Michael Irigoyen
Michael Irigoyen

Reputation: 22947

jQuery - Bind action to parent form, determine what child triggered the bind event

I have a form that has multiple input fields. I'm applying a bind() to this form as so:

$("#form-id").bind('keyup change', function(e) {
   //Run some other code
   alert('here');
});

I would like to determine the ID of which form field triggered the bind event to fire. I've tried working with the Event object sent on the function, however the information doesn't seem to reside there. For example, e.relatedTarget is null.

Is is possible to determine what child element of the form triggered the bind event to be triggered?

Upvotes: 0

Views: 439

Answers (2)

James Allardice
James Allardice

Reputation: 165971

Use the event target:

$("#form-id").bind('keyup change', function(e) {
    var targetId = e.target.id;
});

Here's a working example.

Upvotes: 3

Blazemonger
Blazemonger

Reputation: 92903

http://api.jquery.com/event.target/ -- event.target should be what you want.

Upvotes: 2

Related Questions