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?
2 Answers
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.
3 Comments
Crescent Fresh
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.AnApprentice
seems like that could double fire events? is there a way to make sure that doesn't happen?
Crescent Fresh
@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.