views:

574

answers:

3

Hi,

I want to trigger a javascript function when one of the elements in a specific form has been changed. Is this possible using jquery?

Does anybody has an example how to do this?

thanks,

Bart

+1  A: 

If you have a text field like this:

<input type='text' id='myInput'>

You can do this:

function executeOnChange() {
    alert('new value = ' + $(this).val());    
}
$(function() { // wait for DOM to be ready before trying to bind...
    $('#myInput').change(executeOnChange);
});

Here is an example of this in action.

Please note you have to lose focus on the text field in order for the change event to fire.

If you want to select the input by name instead of ID, you can change $('#myInput') to $('input[name=myName]'). For more on selectors, check out the documentation.

Paolo Bergantino
+1  A: 

Jut write a selector for the items in the form and attach a change handler see http://docs.jquery.com/Events/change#examples

almog.ori
+1  A: 
$(function () { 
  $('form#id input[name=joe]').change( function() { alert('it changed!'); });
} );
austinfromboston
thanks... didn't know it was that simple... just calling the change event on the form.
Fortega