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?
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?
Share Improve this question edited Jul 10, 2010 at 20:53 Crescent Fresh 117k27 gold badges157 silver badges140 bronze badges asked Jul 10, 2010 at 20:46 AnApprenticeAnApprentice 111k201 gold badges636 silver badges1k bronze badges 1- Are you using (at least) jQuery 1.4.2? – Crescent Fresh Commented Jul 10, 2010 at 20:48
2 Answers
Reset to default 14JavaScript 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.
$('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.