I have a page that has multiple forms. Anytime the user clicks an input or modifies text in an input I would like a function to be called. Any ideas on how to do this efficiently and in a way where it doesn't require the form IDs?
A:
$('form input').each(function() {
var val = this.value;
$(this).click(function() { }
$(this).blur(function() {
}
});
You can also use delegate for better performance. It would help seeing your source and your exact needs.
meder
2010-07-10 20:50:23
+2
A:
JavaScript events bubble up. So how about:
$('form').change(function() {
// do something
}).click(function() {
// do something
});
In each case you can query for the element that triggered the event and do what you please.
AndrewDotHay
2010-07-10 20:50:50
does `change` work for inputs in IE?
meder
2010-07-10 20:51:37
`change` is not reliable in IE for radio buttons, checkboxes, `<select>` controls, unless you have jQuery 1.4.2, in which case a properly bubbling `change` event becomes available to IE.
Crescent Fresh
2010-07-10 20:56:07
seems like that could double fire events? is there a way to make sure that doesn't happen?
AnApprentice
2010-07-10 20:56:19
@nobosh: yes I'd pick one (`change` or `click`), not both. I think the example was only meant to show that you can bind to the parent `form` elements, not to each child input.
Crescent Fresh
2010-07-10 21:00:10