views:

77

answers:

2

I've got a form and I need to post data to the server every time a field in the form is changed. How do I apply a function to each child element of the form on blur?

+1  A: 

Try something like this:

$('form#yourform > input').blur(function(){
    // Do Server Request here.
}); 
Nexum
Make sure to hit <select> and <textarea> elements too.
Joseph Mastey
A: 

I've created update() as the function that posts to the server and used the following:

        $( 'input' ).blur( function() {
            update();
        }); 

        $( 'input[type=checkbox]' ).click( function() {
            update();
        });

        $( 'textarea' ).blur( function() {
            update();
        });

        $( 'select' ).change( function() {
            update();
        });
James Inman
If you're using jQuery 1.4.2 you can just do `$('form').change(update)`.
Crescent Fresh
Isn't the $ selector conducive to CSS style selectors? Why not have one single selector to rule them all, and save yourself the function call?$('input, input[type=checkbox], textarea, select').blur(function() { });
Mark Tomlin