4

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?

1
  • Are you using (at least) jQuery 1.4.2? Commented Jul 10, 2010 at 20:48

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
seems like that could double fire events? is there a way to make sure that doesn't happen?
@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.
3
$('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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.