7

I'm using the following snippet to detect every time a particular form input field changes.

$( '#textbox' ).on( 'input', function() {
    // Do something here
});

This particular input field belongs to a form which has checkboxes, radio buttons and more text input fields. Is there a way to detect when there is a change to any of the form's fields?

3
  • 1
    Events bubble up, so you can bind the event handler to the form element: $( 'form' ).on( 'input', function(e) { console.log(e.target); }); Commented Jul 15, 2014 at 13:39
  • What does the 'e' do in function(e)? Commented Jul 15, 2014 at 13:46
  • e is the event object, it's target property refers to the target of the event. Commented Jul 15, 2014 at 13:50

3 Answers 3

15

Try,

$('#Form input').on( 'input', function() {
  //This would be called if any of the input element has got a change inside the form
}); 
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way to accomplish this same solution when there is no 'form' on the page?
@KirbyL.Wallace What is holding that input elements?
what if i add some text to textbox and removed it or cleared it? will this be treated as change or no change occured. How do i accomplish as if no change occured.
9

Try this:

HTML:

<form>
    <p><input type='text' /></p>
    <p><input type='text' /></p>
    <p><input type='checkbox' /></p>
</form>

JQuery:

$('form :input').change(function(){
   alert("Form changed");
});

JSFiddle Demo

Comments

3
$("form :input").change(function() {

});

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.