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?
views:
77answers:
2
+1
A:
Try something like this:
$('form#yourform > input').blur(function(){
// Do Server Request here.
});
Nexum
2010-03-20 14:34:06
Make sure to hit <select> and <textarea> elements too.
Joseph Mastey
2010-03-20 14:42:52
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
2010-03-20 14:43:27
If you're using jQuery 1.4.2 you can just do `$('form').change(update)`.
Crescent Fresh
2010-03-20 14:50:05
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
2010-03-20 14:53:12