How do you handle a form change in jQuery?











up vote
66
down vote

favorite
20












In jQuery, is there a simple way to test if ANY of a form's elements have changed?



EDIT: I should have added that I only need to check on a click() event.



EDIT: Sorry I really should have been more specific! Say I have a form and I have a button with the following:



$('#mybutton').click(function() {
// Here is where is need to test
if(/* FORM has changed */) {
// Do something
}
});


How would I test if the form has changed since it was loaded?










share|improve this question
























  • Do you mean by action of some other script included in the page or as soon as the user has typed some text or clicked a radio/checkbox?
    – FelipeAls
    Jun 11 '10 at 18:51










  • I should have added that I only need to check on a click() event
    – mike
    Jun 11 '10 at 18:55















up vote
66
down vote

favorite
20












In jQuery, is there a simple way to test if ANY of a form's elements have changed?



EDIT: I should have added that I only need to check on a click() event.



EDIT: Sorry I really should have been more specific! Say I have a form and I have a button with the following:



$('#mybutton').click(function() {
// Here is where is need to test
if(/* FORM has changed */) {
// Do something
}
});


How would I test if the form has changed since it was loaded?










share|improve this question
























  • Do you mean by action of some other script included in the page or as soon as the user has typed some text or clicked a radio/checkbox?
    – FelipeAls
    Jun 11 '10 at 18:51










  • I should have added that I only need to check on a click() event
    – mike
    Jun 11 '10 at 18:55













up vote
66
down vote

favorite
20









up vote
66
down vote

favorite
20






20





In jQuery, is there a simple way to test if ANY of a form's elements have changed?



EDIT: I should have added that I only need to check on a click() event.



EDIT: Sorry I really should have been more specific! Say I have a form and I have a button with the following:



$('#mybutton').click(function() {
// Here is where is need to test
if(/* FORM has changed */) {
// Do something
}
});


How would I test if the form has changed since it was loaded?










share|improve this question















In jQuery, is there a simple way to test if ANY of a form's elements have changed?



EDIT: I should have added that I only need to check on a click() event.



EDIT: Sorry I really should have been more specific! Say I have a form and I have a button with the following:



$('#mybutton').click(function() {
// Here is where is need to test
if(/* FORM has changed */) {
// Do something
}
});


How would I test if the form has changed since it was loaded?







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 20 '16 at 18:53









Lemmings19

427723




427723










asked Jun 11 '10 at 18:49









mike

3,523184761




3,523184761












  • Do you mean by action of some other script included in the page or as soon as the user has typed some text or clicked a radio/checkbox?
    – FelipeAls
    Jun 11 '10 at 18:51










  • I should have added that I only need to check on a click() event
    – mike
    Jun 11 '10 at 18:55


















  • Do you mean by action of some other script included in the page or as soon as the user has typed some text or clicked a radio/checkbox?
    – FelipeAls
    Jun 11 '10 at 18:51










  • I should have added that I only need to check on a click() event
    – mike
    Jun 11 '10 at 18:55
















Do you mean by action of some other script included in the page or as soon as the user has typed some text or clicked a radio/checkbox?
– FelipeAls
Jun 11 '10 at 18:51




Do you mean by action of some other script included in the page or as soon as the user has typed some text or clicked a radio/checkbox?
– FelipeAls
Jun 11 '10 at 18:51












I should have added that I only need to check on a click() event
– mike
Jun 11 '10 at 18:55




I should have added that I only need to check on a click() event
– mike
Jun 11 '10 at 18:55












13 Answers
13






active

oldest

votes

















up vote
120
down vote



accepted










You can do this:



$("form :input").change(function() {
$(this).closest('form').data('changed', true);
});
$('#mybutton').click(function() {
if($(this).closest('form').data('changed')) {
//do something
}
});


This rigs a change event handler to inputs in the form, if any of them change it uses .data() to set a changed value to true, then we just check for that value on the click, this assumes that #mybutton is inside the form (if not just replace $(this).closest('form') with $('#myForm')), but you could make it even more generic, like this:



$('.checkChangedbutton').click(function() {
if($(this).closest('form').data('changed')) {
//do something
}
});


References: Updated



According to jQuery this is a filter to select all form controls.



http://api.jquery.com/input-selector/




The :input selector basically selects all form controls.







share|improve this answer



















  • 6




    Does $('form :input') account for the select, textarea and other input types ? or is it specific to the input tag?
    – Val
    May 31 '13 at 9:42






  • 8




    @Val it accounts for all 3, see here: api.jquery.com/input-selector
    – Nick Craver
    May 31 '13 at 10:08






  • 5




    Correct me if I'm wrong, but it seems like, this method will say the form has been changed, even if someone makes a change and then "undoes" their change, manually reverting back to the original values in the form. In this case the form really wouldn't have changed with respect to the data, yet this method would say it had.
    – paul
    Mar 10 '14 at 22:46






  • 1




    Thanks, yea I tried that (it's listed in another answer) but it didn't work for me. It seems like there are some special cases that trip that method up as well. Currently working with a plugin - github.com/codedance/jquery.AreYouSure
    – paul
    Mar 11 '14 at 16:29








  • 1




    Good solution for this case! But if someone here wants to get the state of the form's content (like "IsDirty"), you might want to follow this solution: stackoverflow.com/a/26796840/665783
    – Jacob
    May 5 '17 at 1:55


















up vote
43
down vote













If you want to check if the form data, as it is going to be sent to the server, have changed, you can serialize the form data on page load and compare it to the current form data:



$(function() {

var form_original_data = $("#myform").serialize();

$("#mybutton").click(function() {
if ($("#myform").serialize() != form_original_data) {
// Something changed
}
});

});





share|improve this answer

















  • 5




    Excellent, this is the best solution out here. Thanks.
    – Anonymous
    Sep 7 '12 at 19:05






  • 3




    I used this method once, and it became very slow when the form became bigger. This is a good method for small forms, but it is not the way to go for large forms.
    – mkdevoogd
    Apr 22 '13 at 8:13










  • do you mean forms with files in them?
    – Udi
    Apr 22 '13 at 17:26






  • 1




    This is good, but it would be better if instead of checking each time a control changes, directly check when the form is about to be submitted
    – Ruby Racer
    Aug 13 '14 at 6:19






  • 2




    This is much better, so you can trigger buttons back to disabled, like "delete"
    – EliuX
    Sep 24 '15 at 17:27


















up vote
13
down vote













A real time and simple solution:



$('form').on('keyup change paste', 'input, select, textarea', function(){
console.log('Form changed!');
});





share|improve this answer



















  • 1




    Very good solution. Suitable for as you type form validation.
    – Bijan
    Dec 8 '14 at 14:05






  • 1




    The best solution for me! Thanks a lot.
    – Ivan
    Feb 23 '16 at 6:48






  • 2




    Consider using 'input' instead of 'keyup change' to handle right-mouse-paste, unless you need to support older browsers.
    – TrueWill
    Dec 29 '16 at 20:33


















up vote
10
down vote













You can use multiple selectors to attach a callback to the change event for any form element.



$("input, select").change(function(){
// Something changed
});


EDIT



Since you mentioned you only need this for a click, you can simply modify my original code to this:



$("input, select").click(function(){
// A form element was clicked
});


EDIT #2



Ok, you can set a global that is set once something has been changed like this:



var FORM_HAS_CHANGED = false;

$('#mybutton').click(function() {
if (FORM_HAS_CHANGED) {
// The form has changed
}
});

$("input, select").change(function(){
FORM_HAS_CHANGED = true;
});





share|improve this answer























  • added another edit! sorry
    – mike
    Jun 11 '10 at 19:03










  • +1 for multiple selectors, might also add a textarea
    – Anonymous
    Sep 7 '12 at 18:55


















up vote
3
down vote













Looking at the updated question try something like



$('input, textarea, select').each(function(){
$(this).data("val", $(this).val());
});
$('#button').click(function() {
$('input, textarea, select').each(function(){
if($(this).data("val")!==$(this).val()) alert("Things Changed");
});
});


For the original question use something like



$('input').change(function() {
alert("Things have changed!");
});





share|improve this answer























  • This doesn't check <textarea> or <select> elements.
    – Nick Craver
    Jun 11 '10 at 19:11










  • After your edit...this seems like a lot of extra work and data being moved around, all you need is a boolean set to true when a change happens. Also, this still isn't form-specific, what if there were other forms in the page?
    – Nick Craver
    Jun 11 '10 at 19:19










  • +1 that's a good one, but it needs some work, bind it to form and set init.
    – Anonymous
    Sep 7 '12 at 19:02










  • that's what exactly i thought :) (y) your rocked
    – R K Sharma
    Sep 9 '15 at 11:53


















up vote
2
down vote













$('form :input').change(function() {
// Something has changed
});





share|improve this answer




























    up vote
    2
    down vote













    Here is an elegant solution.



    There is hidden property for each input element on the form that you can use to determine whether or not the value was changed.
    Each type of input has it's own property name. For example




    • for text/textarea it's defaultValue

    • for select it's defaultSelect

    • for checkbox/radio it's defaultChecked


    Here is the example.



    function bindFormChange($form) {

    function touchButtons() {
    var
    changed_objects = ,
    $observable_buttons = $form.find('input[type="submit"], button[type="submit"], button[data-object="reset-form"]');

    changed_objects = $('input:text, input:checkbox, input:radio, textarea, select', $form).map(function () {
    var
    $input = $(this),
    changed = false;

    if ($input.is('input:text') || $input.is('textarea') ) {
    changed = (($input).prop('defaultValue') != $input.val());
    }
    if (!changed && $input.is('select') ) {
    changed = !$('option:selected', $input).prop('defaultSelected');
    }
    if (!changed && $input.is('input:checkbox') || $input.is('input:radio') ) {
    changed = (($input).prop('defaultChecked') != $input.is(':checked'));
    }
    if (changed) {
    return $input.attr('id');
    }

    }).toArray();

    if (changed_objects.length) {
    $observable_buttons.removeAttr('disabled')
    } else {
    $observable_buttons.attr('disabled', 'disabled');
    }
    };
    touchButtons();

    $('input, textarea, select', $form).each(function () {
    var $input = $(this);

    $input.on('keyup change', function () {
    touchButtons();
    });
    });

    };


    Now just loop thru the forms on the page and you should see submit buttons disabled by default and they will be activated ONLY if you indeed will change some input value on the form.



    $('form').each(function () {
    bindFormChange($(this));
    });


    Implementation as a jQuery plugin is here https://github.com/kulbida/jmodifiable






    share|improve this answer






























      up vote
      2
      down vote













      var formStr = JSON.stringify($("#form").serializeArray());
      ...
      function Submit(){
      var newformStr = JSON.stringify($("#form").serializeArray());
      if (formStr != newformStr){
      ...
      formChangedfunct();
      ...
      }
      else {
      ...
      formUnchangedfunct();
      ...
      }
      }





      share|improve this answer





















      • Yeah all these years later I don't even use jQuery anymore. Observers and bindings all the way now! Nice though for anyone using jQuery.
        – mike
        Jan 22 at 15:36




















      up vote
      1
      down vote













      You need jQuery Form Observe plugin. That's what you are looking for.






      share|improve this answer




























        up vote
        1
        down vote













        Extending Udi's answer, this only checks on form submission, not on every input change.



        $(document).ready( function () {
        var form_data = $('#myform').serialize();
        $('#myform').submit(function () {
        if ( form_data == $(this).serialize() ) {
        alert('no change');
        } else {
        alert('change');
        }
        });
        });





        share|improve this answer




























          up vote
          0
          down vote













          First, I'd add a hidden input to your form to track the state of the form. Then, I'd use this jQuery snippet to set the value of the hidden input when something on the form changes:



              $("form")
          .find("input")
          .change(function(){
          if ($("#hdnFormChanged").val() == "no")
          {
          $("#hdnFormChanged").val("yes");
          }
          });


          When your button is clicked, you can check the state of your hidden input:



          $("#Button").click(function(){
          if($("#hdnFormChanged").val() == "yes")
          {
          // handler code here...
          }
          });





          share|improve this answer



















          • 6




            No reason for extra inputs :) Check out .data()
            – Nick Craver
            Jun 11 '10 at 19:10










          • I learn something new every day!
            – JMP
            Jun 11 '10 at 19:41










          • -1 totally agree with Nick Crave.
            – Anonymous
            Sep 7 '12 at 19:02


















          up vote
          0
          down vote
















          $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
          $("#result").html($(this).val());
          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <h2>Form "your_form_name"</h2>
          <form name="your_form_name">
          <input type="text" name="one_a" id="one_a" value="AAAAAAAA" />
          <input type="text" name="one_b" id="one_b" value="BBBBBBBB" />
          <input type="text" name="one_c" id="one_c" value="CCCCCCCC" />
          <select name="one_d">
          <option value="111111">111111</option>
          <option value="222222">222222</option>
          <option value="333333">333333</option>
          </select>
          </form>
          <hr/>
          <h2>Form "your_other_form_name"</h2>
          <form name="your_other_form_name">
          <input type="text" name="two_a" id="two_a" value="DDDDDDDD" />
          <input type="text" name="two_b" id="two_b" value="EEEEEEEE" />
          <input type="text" name="two_c" id="two_c" value="FFFFFFFF" />
          <input type="text" name="two_d" id="two_d" value="GGGGGGGG" />
          <input type="text" name="two_e" id="two_f" value="HHHHHHHH" />
          <input type="text" name="two_f" id="two_e" value="IIIIIIII" />
          <select name="two_g">
          <option value="444444">444444</option>
          <option value="555555">555555</option>
          <option value="666666">666666</option>
          </select>
          </form>
          <h2>Result</h2>
          <div id="result">
          <h2>Click on a field..</h2>
          </div>





          In addition to above @JoeD's answer.



          If you want to target fields in a particular form (assuming there are more than one forms) than just fields, you can use the following code:



          $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
          // A form element was clicked
          });





          share|improve this answer























          • Just FYI, this probably won't do what you want - it targets the input elements of the "your_form_name" form, and all selects in the document. Also, for performance reasons, it is always better to use $.fn.find rather than just targeting descendent selectors, e.g. $('form').find('input,select')
            – dgo
            Jan 29 at 20:35










          • @user1167442 How about if we have more than one form? My example is for a situation when there's more than one form in a single page.
            – Anjana Silva
            Jan 30 at 8:49






          • 1




            But still - your example will find all the input elements that are children of the "your_form_name" form, and then find all selects whether they are children of "your_form_name" or any form at all. Your example won't work for multiple forms, unless you are only targeting the selects of those forms. Also, if you are referring to the performance piece, it is always better to use $.fn.find - faster than even $.fn.filter - to reference children rather than using descendent selectors. vaughnroyko.com/the-real-scoop-on-jquery-find-performance
            – dgo
            Jan 30 at 18:02






          • 1




            @user1167442, you are absolutely right. I missed the fact that select is being targeted regardless of what form you are in. I have corrected that. Thank you very much for pointing out. Cheers!
            – Anjana Silva
            Jan 31 at 9:42


















          up vote
          0
          down vote













          Try this:



          <script>
          var form_original_data = $("form").serialize();
          var form_submit=false;
          $('[type="submit"]').click(function() {
          form_submit=true;
          });
          window.onbeforeunload = function() {
          //console.log($("form").submit());
          if ($("form").serialize() != form_original_data && form_submit==false) {
          return "Do you really want to leave without saving?";
          }
          };
          </script>





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3025396%2fhow-do-you-handle-a-form-change-in-jquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            13 Answers
            13






            active

            oldest

            votes








            13 Answers
            13






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            120
            down vote



            accepted










            You can do this:



            $("form :input").change(function() {
            $(this).closest('form').data('changed', true);
            });
            $('#mybutton').click(function() {
            if($(this).closest('form').data('changed')) {
            //do something
            }
            });


            This rigs a change event handler to inputs in the form, if any of them change it uses .data() to set a changed value to true, then we just check for that value on the click, this assumes that #mybutton is inside the form (if not just replace $(this).closest('form') with $('#myForm')), but you could make it even more generic, like this:



            $('.checkChangedbutton').click(function() {
            if($(this).closest('form').data('changed')) {
            //do something
            }
            });


            References: Updated



            According to jQuery this is a filter to select all form controls.



            http://api.jquery.com/input-selector/




            The :input selector basically selects all form controls.







            share|improve this answer



















            • 6




              Does $('form :input') account for the select, textarea and other input types ? or is it specific to the input tag?
              – Val
              May 31 '13 at 9:42






            • 8




              @Val it accounts for all 3, see here: api.jquery.com/input-selector
              – Nick Craver
              May 31 '13 at 10:08






            • 5




              Correct me if I'm wrong, but it seems like, this method will say the form has been changed, even if someone makes a change and then "undoes" their change, manually reverting back to the original values in the form. In this case the form really wouldn't have changed with respect to the data, yet this method would say it had.
              – paul
              Mar 10 '14 at 22:46






            • 1




              Thanks, yea I tried that (it's listed in another answer) but it didn't work for me. It seems like there are some special cases that trip that method up as well. Currently working with a plugin - github.com/codedance/jquery.AreYouSure
              – paul
              Mar 11 '14 at 16:29








            • 1




              Good solution for this case! But if someone here wants to get the state of the form's content (like "IsDirty"), you might want to follow this solution: stackoverflow.com/a/26796840/665783
              – Jacob
              May 5 '17 at 1:55















            up vote
            120
            down vote



            accepted










            You can do this:



            $("form :input").change(function() {
            $(this).closest('form').data('changed', true);
            });
            $('#mybutton').click(function() {
            if($(this).closest('form').data('changed')) {
            //do something
            }
            });


            This rigs a change event handler to inputs in the form, if any of them change it uses .data() to set a changed value to true, then we just check for that value on the click, this assumes that #mybutton is inside the form (if not just replace $(this).closest('form') with $('#myForm')), but you could make it even more generic, like this:



            $('.checkChangedbutton').click(function() {
            if($(this).closest('form').data('changed')) {
            //do something
            }
            });


            References: Updated



            According to jQuery this is a filter to select all form controls.



            http://api.jquery.com/input-selector/




            The :input selector basically selects all form controls.







            share|improve this answer



















            • 6




              Does $('form :input') account for the select, textarea and other input types ? or is it specific to the input tag?
              – Val
              May 31 '13 at 9:42






            • 8




              @Val it accounts for all 3, see here: api.jquery.com/input-selector
              – Nick Craver
              May 31 '13 at 10:08






            • 5




              Correct me if I'm wrong, but it seems like, this method will say the form has been changed, even if someone makes a change and then "undoes" their change, manually reverting back to the original values in the form. In this case the form really wouldn't have changed with respect to the data, yet this method would say it had.
              – paul
              Mar 10 '14 at 22:46






            • 1




              Thanks, yea I tried that (it's listed in another answer) but it didn't work for me. It seems like there are some special cases that trip that method up as well. Currently working with a plugin - github.com/codedance/jquery.AreYouSure
              – paul
              Mar 11 '14 at 16:29








            • 1




              Good solution for this case! But if someone here wants to get the state of the form's content (like "IsDirty"), you might want to follow this solution: stackoverflow.com/a/26796840/665783
              – Jacob
              May 5 '17 at 1:55













            up vote
            120
            down vote



            accepted







            up vote
            120
            down vote



            accepted






            You can do this:



            $("form :input").change(function() {
            $(this).closest('form').data('changed', true);
            });
            $('#mybutton').click(function() {
            if($(this).closest('form').data('changed')) {
            //do something
            }
            });


            This rigs a change event handler to inputs in the form, if any of them change it uses .data() to set a changed value to true, then we just check for that value on the click, this assumes that #mybutton is inside the form (if not just replace $(this).closest('form') with $('#myForm')), but you could make it even more generic, like this:



            $('.checkChangedbutton').click(function() {
            if($(this).closest('form').data('changed')) {
            //do something
            }
            });


            References: Updated



            According to jQuery this is a filter to select all form controls.



            http://api.jquery.com/input-selector/




            The :input selector basically selects all form controls.







            share|improve this answer














            You can do this:



            $("form :input").change(function() {
            $(this).closest('form').data('changed', true);
            });
            $('#mybutton').click(function() {
            if($(this).closest('form').data('changed')) {
            //do something
            }
            });


            This rigs a change event handler to inputs in the form, if any of them change it uses .data() to set a changed value to true, then we just check for that value on the click, this assumes that #mybutton is inside the form (if not just replace $(this).closest('form') with $('#myForm')), but you could make it even more generic, like this:



            $('.checkChangedbutton').click(function() {
            if($(this).closest('form').data('changed')) {
            //do something
            }
            });


            References: Updated



            According to jQuery this is a filter to select all form controls.



            http://api.jquery.com/input-selector/




            The :input selector basically selects all form controls.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 31 '13 at 9:47









            Val

            11k1981134




            11k1981134










            answered Jun 11 '10 at 19:05









            Nick Craver

            526k11411891097




            526k11411891097








            • 6




              Does $('form :input') account for the select, textarea and other input types ? or is it specific to the input tag?
              – Val
              May 31 '13 at 9:42






            • 8




              @Val it accounts for all 3, see here: api.jquery.com/input-selector
              – Nick Craver
              May 31 '13 at 10:08






            • 5




              Correct me if I'm wrong, but it seems like, this method will say the form has been changed, even if someone makes a change and then "undoes" their change, manually reverting back to the original values in the form. In this case the form really wouldn't have changed with respect to the data, yet this method would say it had.
              – paul
              Mar 10 '14 at 22:46






            • 1




              Thanks, yea I tried that (it's listed in another answer) but it didn't work for me. It seems like there are some special cases that trip that method up as well. Currently working with a plugin - github.com/codedance/jquery.AreYouSure
              – paul
              Mar 11 '14 at 16:29








            • 1




              Good solution for this case! But if someone here wants to get the state of the form's content (like "IsDirty"), you might want to follow this solution: stackoverflow.com/a/26796840/665783
              – Jacob
              May 5 '17 at 1:55














            • 6




              Does $('form :input') account for the select, textarea and other input types ? or is it specific to the input tag?
              – Val
              May 31 '13 at 9:42






            • 8




              @Val it accounts for all 3, see here: api.jquery.com/input-selector
              – Nick Craver
              May 31 '13 at 10:08






            • 5




              Correct me if I'm wrong, but it seems like, this method will say the form has been changed, even if someone makes a change and then "undoes" their change, manually reverting back to the original values in the form. In this case the form really wouldn't have changed with respect to the data, yet this method would say it had.
              – paul
              Mar 10 '14 at 22:46






            • 1




              Thanks, yea I tried that (it's listed in another answer) but it didn't work for me. It seems like there are some special cases that trip that method up as well. Currently working with a plugin - github.com/codedance/jquery.AreYouSure
              – paul
              Mar 11 '14 at 16:29








            • 1




              Good solution for this case! But if someone here wants to get the state of the form's content (like "IsDirty"), you might want to follow this solution: stackoverflow.com/a/26796840/665783
              – Jacob
              May 5 '17 at 1:55








            6




            6




            Does $('form :input') account for the select, textarea and other input types ? or is it specific to the input tag?
            – Val
            May 31 '13 at 9:42




            Does $('form :input') account for the select, textarea and other input types ? or is it specific to the input tag?
            – Val
            May 31 '13 at 9:42




            8




            8




            @Val it accounts for all 3, see here: api.jquery.com/input-selector
            – Nick Craver
            May 31 '13 at 10:08




            @Val it accounts for all 3, see here: api.jquery.com/input-selector
            – Nick Craver
            May 31 '13 at 10:08




            5




            5




            Correct me if I'm wrong, but it seems like, this method will say the form has been changed, even if someone makes a change and then "undoes" their change, manually reverting back to the original values in the form. In this case the form really wouldn't have changed with respect to the data, yet this method would say it had.
            – paul
            Mar 10 '14 at 22:46




            Correct me if I'm wrong, but it seems like, this method will say the form has been changed, even if someone makes a change and then "undoes" their change, manually reverting back to the original values in the form. In this case the form really wouldn't have changed with respect to the data, yet this method would say it had.
            – paul
            Mar 10 '14 at 22:46




            1




            1




            Thanks, yea I tried that (it's listed in another answer) but it didn't work for me. It seems like there are some special cases that trip that method up as well. Currently working with a plugin - github.com/codedance/jquery.AreYouSure
            – paul
            Mar 11 '14 at 16:29






            Thanks, yea I tried that (it's listed in another answer) but it didn't work for me. It seems like there are some special cases that trip that method up as well. Currently working with a plugin - github.com/codedance/jquery.AreYouSure
            – paul
            Mar 11 '14 at 16:29






            1




            1




            Good solution for this case! But if someone here wants to get the state of the form's content (like "IsDirty"), you might want to follow this solution: stackoverflow.com/a/26796840/665783
            – Jacob
            May 5 '17 at 1:55




            Good solution for this case! But if someone here wants to get the state of the form's content (like "IsDirty"), you might want to follow this solution: stackoverflow.com/a/26796840/665783
            – Jacob
            May 5 '17 at 1:55












            up vote
            43
            down vote













            If you want to check if the form data, as it is going to be sent to the server, have changed, you can serialize the form data on page load and compare it to the current form data:



            $(function() {

            var form_original_data = $("#myform").serialize();

            $("#mybutton").click(function() {
            if ($("#myform").serialize() != form_original_data) {
            // Something changed
            }
            });

            });





            share|improve this answer

















            • 5




              Excellent, this is the best solution out here. Thanks.
              – Anonymous
              Sep 7 '12 at 19:05






            • 3




              I used this method once, and it became very slow when the form became bigger. This is a good method for small forms, but it is not the way to go for large forms.
              – mkdevoogd
              Apr 22 '13 at 8:13










            • do you mean forms with files in them?
              – Udi
              Apr 22 '13 at 17:26






            • 1




              This is good, but it would be better if instead of checking each time a control changes, directly check when the form is about to be submitted
              – Ruby Racer
              Aug 13 '14 at 6:19






            • 2




              This is much better, so you can trigger buttons back to disabled, like "delete"
              – EliuX
              Sep 24 '15 at 17:27















            up vote
            43
            down vote













            If you want to check if the form data, as it is going to be sent to the server, have changed, you can serialize the form data on page load and compare it to the current form data:



            $(function() {

            var form_original_data = $("#myform").serialize();

            $("#mybutton").click(function() {
            if ($("#myform").serialize() != form_original_data) {
            // Something changed
            }
            });

            });





            share|improve this answer

















            • 5




              Excellent, this is the best solution out here. Thanks.
              – Anonymous
              Sep 7 '12 at 19:05






            • 3




              I used this method once, and it became very slow when the form became bigger. This is a good method for small forms, but it is not the way to go for large forms.
              – mkdevoogd
              Apr 22 '13 at 8:13










            • do you mean forms with files in them?
              – Udi
              Apr 22 '13 at 17:26






            • 1




              This is good, but it would be better if instead of checking each time a control changes, directly check when the form is about to be submitted
              – Ruby Racer
              Aug 13 '14 at 6:19






            • 2




              This is much better, so you can trigger buttons back to disabled, like "delete"
              – EliuX
              Sep 24 '15 at 17:27













            up vote
            43
            down vote










            up vote
            43
            down vote









            If you want to check if the form data, as it is going to be sent to the server, have changed, you can serialize the form data on page load and compare it to the current form data:



            $(function() {

            var form_original_data = $("#myform").serialize();

            $("#mybutton").click(function() {
            if ($("#myform").serialize() != form_original_data) {
            // Something changed
            }
            });

            });





            share|improve this answer












            If you want to check if the form data, as it is going to be sent to the server, have changed, you can serialize the form data on page load and compare it to the current form data:



            $(function() {

            var form_original_data = $("#myform").serialize();

            $("#mybutton").click(function() {
            if ($("#myform").serialize() != form_original_data) {
            // Something changed
            }
            });

            });






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 4 '11 at 7:53









            Udi

            17.4k46791




            17.4k46791








            • 5




              Excellent, this is the best solution out here. Thanks.
              – Anonymous
              Sep 7 '12 at 19:05






            • 3




              I used this method once, and it became very slow when the form became bigger. This is a good method for small forms, but it is not the way to go for large forms.
              – mkdevoogd
              Apr 22 '13 at 8:13










            • do you mean forms with files in them?
              – Udi
              Apr 22 '13 at 17:26






            • 1




              This is good, but it would be better if instead of checking each time a control changes, directly check when the form is about to be submitted
              – Ruby Racer
              Aug 13 '14 at 6:19






            • 2




              This is much better, so you can trigger buttons back to disabled, like "delete"
              – EliuX
              Sep 24 '15 at 17:27














            • 5




              Excellent, this is the best solution out here. Thanks.
              – Anonymous
              Sep 7 '12 at 19:05






            • 3




              I used this method once, and it became very slow when the form became bigger. This is a good method for small forms, but it is not the way to go for large forms.
              – mkdevoogd
              Apr 22 '13 at 8:13










            • do you mean forms with files in them?
              – Udi
              Apr 22 '13 at 17:26






            • 1




              This is good, but it would be better if instead of checking each time a control changes, directly check when the form is about to be submitted
              – Ruby Racer
              Aug 13 '14 at 6:19






            • 2




              This is much better, so you can trigger buttons back to disabled, like "delete"
              – EliuX
              Sep 24 '15 at 17:27








            5




            5




            Excellent, this is the best solution out here. Thanks.
            – Anonymous
            Sep 7 '12 at 19:05




            Excellent, this is the best solution out here. Thanks.
            – Anonymous
            Sep 7 '12 at 19:05




            3




            3




            I used this method once, and it became very slow when the form became bigger. This is a good method for small forms, but it is not the way to go for large forms.
            – mkdevoogd
            Apr 22 '13 at 8:13




            I used this method once, and it became very slow when the form became bigger. This is a good method for small forms, but it is not the way to go for large forms.
            – mkdevoogd
            Apr 22 '13 at 8:13












            do you mean forms with files in them?
            – Udi
            Apr 22 '13 at 17:26




            do you mean forms with files in them?
            – Udi
            Apr 22 '13 at 17:26




            1




            1




            This is good, but it would be better if instead of checking each time a control changes, directly check when the form is about to be submitted
            – Ruby Racer
            Aug 13 '14 at 6:19




            This is good, but it would be better if instead of checking each time a control changes, directly check when the form is about to be submitted
            – Ruby Racer
            Aug 13 '14 at 6:19




            2




            2




            This is much better, so you can trigger buttons back to disabled, like "delete"
            – EliuX
            Sep 24 '15 at 17:27




            This is much better, so you can trigger buttons back to disabled, like "delete"
            – EliuX
            Sep 24 '15 at 17:27










            up vote
            13
            down vote













            A real time and simple solution:



            $('form').on('keyup change paste', 'input, select, textarea', function(){
            console.log('Form changed!');
            });





            share|improve this answer



















            • 1




              Very good solution. Suitable for as you type form validation.
              – Bijan
              Dec 8 '14 at 14:05






            • 1




              The best solution for me! Thanks a lot.
              – Ivan
              Feb 23 '16 at 6:48






            • 2




              Consider using 'input' instead of 'keyup change' to handle right-mouse-paste, unless you need to support older browsers.
              – TrueWill
              Dec 29 '16 at 20:33















            up vote
            13
            down vote













            A real time and simple solution:



            $('form').on('keyup change paste', 'input, select, textarea', function(){
            console.log('Form changed!');
            });





            share|improve this answer



















            • 1




              Very good solution. Suitable for as you type form validation.
              – Bijan
              Dec 8 '14 at 14:05






            • 1




              The best solution for me! Thanks a lot.
              – Ivan
              Feb 23 '16 at 6:48






            • 2




              Consider using 'input' instead of 'keyup change' to handle right-mouse-paste, unless you need to support older browsers.
              – TrueWill
              Dec 29 '16 at 20:33













            up vote
            13
            down vote










            up vote
            13
            down vote









            A real time and simple solution:



            $('form').on('keyup change paste', 'input, select, textarea', function(){
            console.log('Form changed!');
            });





            share|improve this answer














            A real time and simple solution:



            $('form').on('keyup change paste', 'input, select, textarea', function(){
            console.log('Form changed!');
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 4 at 18:17

























            answered May 16 '14 at 13:14









            Marcio Mazzucato

            5,11734865




            5,11734865








            • 1




              Very good solution. Suitable for as you type form validation.
              – Bijan
              Dec 8 '14 at 14:05






            • 1




              The best solution for me! Thanks a lot.
              – Ivan
              Feb 23 '16 at 6:48






            • 2




              Consider using 'input' instead of 'keyup change' to handle right-mouse-paste, unless you need to support older browsers.
              – TrueWill
              Dec 29 '16 at 20:33














            • 1




              Very good solution. Suitable for as you type form validation.
              – Bijan
              Dec 8 '14 at 14:05






            • 1




              The best solution for me! Thanks a lot.
              – Ivan
              Feb 23 '16 at 6:48






            • 2




              Consider using 'input' instead of 'keyup change' to handle right-mouse-paste, unless you need to support older browsers.
              – TrueWill
              Dec 29 '16 at 20:33








            1




            1




            Very good solution. Suitable for as you type form validation.
            – Bijan
            Dec 8 '14 at 14:05




            Very good solution. Suitable for as you type form validation.
            – Bijan
            Dec 8 '14 at 14:05




            1




            1




            The best solution for me! Thanks a lot.
            – Ivan
            Feb 23 '16 at 6:48




            The best solution for me! Thanks a lot.
            – Ivan
            Feb 23 '16 at 6:48




            2




            2




            Consider using 'input' instead of 'keyup change' to handle right-mouse-paste, unless you need to support older browsers.
            – TrueWill
            Dec 29 '16 at 20:33




            Consider using 'input' instead of 'keyup change' to handle right-mouse-paste, unless you need to support older browsers.
            – TrueWill
            Dec 29 '16 at 20:33










            up vote
            10
            down vote













            You can use multiple selectors to attach a callback to the change event for any form element.



            $("input, select").change(function(){
            // Something changed
            });


            EDIT



            Since you mentioned you only need this for a click, you can simply modify my original code to this:



            $("input, select").click(function(){
            // A form element was clicked
            });


            EDIT #2



            Ok, you can set a global that is set once something has been changed like this:



            var FORM_HAS_CHANGED = false;

            $('#mybutton').click(function() {
            if (FORM_HAS_CHANGED) {
            // The form has changed
            }
            });

            $("input, select").change(function(){
            FORM_HAS_CHANGED = true;
            });





            share|improve this answer























            • added another edit! sorry
              – mike
              Jun 11 '10 at 19:03










            • +1 for multiple selectors, might also add a textarea
              – Anonymous
              Sep 7 '12 at 18:55















            up vote
            10
            down vote













            You can use multiple selectors to attach a callback to the change event for any form element.



            $("input, select").change(function(){
            // Something changed
            });


            EDIT



            Since you mentioned you only need this for a click, you can simply modify my original code to this:



            $("input, select").click(function(){
            // A form element was clicked
            });


            EDIT #2



            Ok, you can set a global that is set once something has been changed like this:



            var FORM_HAS_CHANGED = false;

            $('#mybutton').click(function() {
            if (FORM_HAS_CHANGED) {
            // The form has changed
            }
            });

            $("input, select").change(function(){
            FORM_HAS_CHANGED = true;
            });





            share|improve this answer























            • added another edit! sorry
              – mike
              Jun 11 '10 at 19:03










            • +1 for multiple selectors, might also add a textarea
              – Anonymous
              Sep 7 '12 at 18:55













            up vote
            10
            down vote










            up vote
            10
            down vote









            You can use multiple selectors to attach a callback to the change event for any form element.



            $("input, select").change(function(){
            // Something changed
            });


            EDIT



            Since you mentioned you only need this for a click, you can simply modify my original code to this:



            $("input, select").click(function(){
            // A form element was clicked
            });


            EDIT #2



            Ok, you can set a global that is set once something has been changed like this:



            var FORM_HAS_CHANGED = false;

            $('#mybutton').click(function() {
            if (FORM_HAS_CHANGED) {
            // The form has changed
            }
            });

            $("input, select").change(function(){
            FORM_HAS_CHANGED = true;
            });





            share|improve this answer














            You can use multiple selectors to attach a callback to the change event for any form element.



            $("input, select").change(function(){
            // Something changed
            });


            EDIT



            Since you mentioned you only need this for a click, you can simply modify my original code to this:



            $("input, select").click(function(){
            // A form element was clicked
            });


            EDIT #2



            Ok, you can set a global that is set once something has been changed like this:



            var FORM_HAS_CHANGED = false;

            $('#mybutton').click(function() {
            if (FORM_HAS_CHANGED) {
            // The form has changed
            }
            });

            $("input, select").change(function(){
            FORM_HAS_CHANGED = true;
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 11 '10 at 19:10

























            answered Jun 11 '10 at 18:52









            Joe D

            1,48821214




            1,48821214












            • added another edit! sorry
              – mike
              Jun 11 '10 at 19:03










            • +1 for multiple selectors, might also add a textarea
              – Anonymous
              Sep 7 '12 at 18:55


















            • added another edit! sorry
              – mike
              Jun 11 '10 at 19:03










            • +1 for multiple selectors, might also add a textarea
              – Anonymous
              Sep 7 '12 at 18:55
















            added another edit! sorry
            – mike
            Jun 11 '10 at 19:03




            added another edit! sorry
            – mike
            Jun 11 '10 at 19:03












            +1 for multiple selectors, might also add a textarea
            – Anonymous
            Sep 7 '12 at 18:55




            +1 for multiple selectors, might also add a textarea
            – Anonymous
            Sep 7 '12 at 18:55










            up vote
            3
            down vote













            Looking at the updated question try something like



            $('input, textarea, select').each(function(){
            $(this).data("val", $(this).val());
            });
            $('#button').click(function() {
            $('input, textarea, select').each(function(){
            if($(this).data("val")!==$(this).val()) alert("Things Changed");
            });
            });


            For the original question use something like



            $('input').change(function() {
            alert("Things have changed!");
            });





            share|improve this answer























            • This doesn't check <textarea> or <select> elements.
              – Nick Craver
              Jun 11 '10 at 19:11










            • After your edit...this seems like a lot of extra work and data being moved around, all you need is a boolean set to true when a change happens. Also, this still isn't form-specific, what if there were other forms in the page?
              – Nick Craver
              Jun 11 '10 at 19:19










            • +1 that's a good one, but it needs some work, bind it to form and set init.
              – Anonymous
              Sep 7 '12 at 19:02










            • that's what exactly i thought :) (y) your rocked
              – R K Sharma
              Sep 9 '15 at 11:53















            up vote
            3
            down vote













            Looking at the updated question try something like



            $('input, textarea, select').each(function(){
            $(this).data("val", $(this).val());
            });
            $('#button').click(function() {
            $('input, textarea, select').each(function(){
            if($(this).data("val")!==$(this).val()) alert("Things Changed");
            });
            });


            For the original question use something like



            $('input').change(function() {
            alert("Things have changed!");
            });





            share|improve this answer























            • This doesn't check <textarea> or <select> elements.
              – Nick Craver
              Jun 11 '10 at 19:11










            • After your edit...this seems like a lot of extra work and data being moved around, all you need is a boolean set to true when a change happens. Also, this still isn't form-specific, what if there were other forms in the page?
              – Nick Craver
              Jun 11 '10 at 19:19










            • +1 that's a good one, but it needs some work, bind it to form and set init.
              – Anonymous
              Sep 7 '12 at 19:02










            • that's what exactly i thought :) (y) your rocked
              – R K Sharma
              Sep 9 '15 at 11:53













            up vote
            3
            down vote










            up vote
            3
            down vote









            Looking at the updated question try something like



            $('input, textarea, select').each(function(){
            $(this).data("val", $(this).val());
            });
            $('#button').click(function() {
            $('input, textarea, select').each(function(){
            if($(this).data("val")!==$(this).val()) alert("Things Changed");
            });
            });


            For the original question use something like



            $('input').change(function() {
            alert("Things have changed!");
            });





            share|improve this answer














            Looking at the updated question try something like



            $('input, textarea, select').each(function(){
            $(this).data("val", $(this).val());
            });
            $('#button').click(function() {
            $('input, textarea, select').each(function(){
            if($(this).data("val")!==$(this).val()) alert("Things Changed");
            });
            });


            For the original question use something like



            $('input').change(function() {
            alert("Things have changed!");
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 9 '15 at 20:13

























            answered Jun 11 '10 at 18:52









            Pez Cuckow

            9,3511363114




            9,3511363114












            • This doesn't check <textarea> or <select> elements.
              – Nick Craver
              Jun 11 '10 at 19:11










            • After your edit...this seems like a lot of extra work and data being moved around, all you need is a boolean set to true when a change happens. Also, this still isn't form-specific, what if there were other forms in the page?
              – Nick Craver
              Jun 11 '10 at 19:19










            • +1 that's a good one, but it needs some work, bind it to form and set init.
              – Anonymous
              Sep 7 '12 at 19:02










            • that's what exactly i thought :) (y) your rocked
              – R K Sharma
              Sep 9 '15 at 11:53


















            • This doesn't check <textarea> or <select> elements.
              – Nick Craver
              Jun 11 '10 at 19:11










            • After your edit...this seems like a lot of extra work and data being moved around, all you need is a boolean set to true when a change happens. Also, this still isn't form-specific, what if there were other forms in the page?
              – Nick Craver
              Jun 11 '10 at 19:19










            • +1 that's a good one, but it needs some work, bind it to form and set init.
              – Anonymous
              Sep 7 '12 at 19:02










            • that's what exactly i thought :) (y) your rocked
              – R K Sharma
              Sep 9 '15 at 11:53
















            This doesn't check <textarea> or <select> elements.
            – Nick Craver
            Jun 11 '10 at 19:11




            This doesn't check <textarea> or <select> elements.
            – Nick Craver
            Jun 11 '10 at 19:11












            After your edit...this seems like a lot of extra work and data being moved around, all you need is a boolean set to true when a change happens. Also, this still isn't form-specific, what if there were other forms in the page?
            – Nick Craver
            Jun 11 '10 at 19:19




            After your edit...this seems like a lot of extra work and data being moved around, all you need is a boolean set to true when a change happens. Also, this still isn't form-specific, what if there were other forms in the page?
            – Nick Craver
            Jun 11 '10 at 19:19












            +1 that's a good one, but it needs some work, bind it to form and set init.
            – Anonymous
            Sep 7 '12 at 19:02




            +1 that's a good one, but it needs some work, bind it to form and set init.
            – Anonymous
            Sep 7 '12 at 19:02












            that's what exactly i thought :) (y) your rocked
            – R K Sharma
            Sep 9 '15 at 11:53




            that's what exactly i thought :) (y) your rocked
            – R K Sharma
            Sep 9 '15 at 11:53










            up vote
            2
            down vote













            $('form :input').change(function() {
            // Something has changed
            });





            share|improve this answer

























              up vote
              2
              down vote













              $('form :input').change(function() {
              // Something has changed
              });





              share|improve this answer























                up vote
                2
                down vote










                up vote
                2
                down vote









                $('form :input').change(function() {
                // Something has changed
                });





                share|improve this answer












                $('form :input').change(function() {
                // Something has changed
                });






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 11 '10 at 18:50









                VoteyDisciple

                31.9k27585




                31.9k27585






















                    up vote
                    2
                    down vote













                    Here is an elegant solution.



                    There is hidden property for each input element on the form that you can use to determine whether or not the value was changed.
                    Each type of input has it's own property name. For example




                    • for text/textarea it's defaultValue

                    • for select it's defaultSelect

                    • for checkbox/radio it's defaultChecked


                    Here is the example.



                    function bindFormChange($form) {

                    function touchButtons() {
                    var
                    changed_objects = ,
                    $observable_buttons = $form.find('input[type="submit"], button[type="submit"], button[data-object="reset-form"]');

                    changed_objects = $('input:text, input:checkbox, input:radio, textarea, select', $form).map(function () {
                    var
                    $input = $(this),
                    changed = false;

                    if ($input.is('input:text') || $input.is('textarea') ) {
                    changed = (($input).prop('defaultValue') != $input.val());
                    }
                    if (!changed && $input.is('select') ) {
                    changed = !$('option:selected', $input).prop('defaultSelected');
                    }
                    if (!changed && $input.is('input:checkbox') || $input.is('input:radio') ) {
                    changed = (($input).prop('defaultChecked') != $input.is(':checked'));
                    }
                    if (changed) {
                    return $input.attr('id');
                    }

                    }).toArray();

                    if (changed_objects.length) {
                    $observable_buttons.removeAttr('disabled')
                    } else {
                    $observable_buttons.attr('disabled', 'disabled');
                    }
                    };
                    touchButtons();

                    $('input, textarea, select', $form).each(function () {
                    var $input = $(this);

                    $input.on('keyup change', function () {
                    touchButtons();
                    });
                    });

                    };


                    Now just loop thru the forms on the page and you should see submit buttons disabled by default and they will be activated ONLY if you indeed will change some input value on the form.



                    $('form').each(function () {
                    bindFormChange($(this));
                    });


                    Implementation as a jQuery plugin is here https://github.com/kulbida/jmodifiable






                    share|improve this answer



























                      up vote
                      2
                      down vote













                      Here is an elegant solution.



                      There is hidden property for each input element on the form that you can use to determine whether or not the value was changed.
                      Each type of input has it's own property name. For example




                      • for text/textarea it's defaultValue

                      • for select it's defaultSelect

                      • for checkbox/radio it's defaultChecked


                      Here is the example.



                      function bindFormChange($form) {

                      function touchButtons() {
                      var
                      changed_objects = ,
                      $observable_buttons = $form.find('input[type="submit"], button[type="submit"], button[data-object="reset-form"]');

                      changed_objects = $('input:text, input:checkbox, input:radio, textarea, select', $form).map(function () {
                      var
                      $input = $(this),
                      changed = false;

                      if ($input.is('input:text') || $input.is('textarea') ) {
                      changed = (($input).prop('defaultValue') != $input.val());
                      }
                      if (!changed && $input.is('select') ) {
                      changed = !$('option:selected', $input).prop('defaultSelected');
                      }
                      if (!changed && $input.is('input:checkbox') || $input.is('input:radio') ) {
                      changed = (($input).prop('defaultChecked') != $input.is(':checked'));
                      }
                      if (changed) {
                      return $input.attr('id');
                      }

                      }).toArray();

                      if (changed_objects.length) {
                      $observable_buttons.removeAttr('disabled')
                      } else {
                      $observable_buttons.attr('disabled', 'disabled');
                      }
                      };
                      touchButtons();

                      $('input, textarea, select', $form).each(function () {
                      var $input = $(this);

                      $input.on('keyup change', function () {
                      touchButtons();
                      });
                      });

                      };


                      Now just loop thru the forms on the page and you should see submit buttons disabled by default and they will be activated ONLY if you indeed will change some input value on the form.



                      $('form').each(function () {
                      bindFormChange($(this));
                      });


                      Implementation as a jQuery plugin is here https://github.com/kulbida/jmodifiable






                      share|improve this answer

























                        up vote
                        2
                        down vote










                        up vote
                        2
                        down vote









                        Here is an elegant solution.



                        There is hidden property for each input element on the form that you can use to determine whether or not the value was changed.
                        Each type of input has it's own property name. For example




                        • for text/textarea it's defaultValue

                        • for select it's defaultSelect

                        • for checkbox/radio it's defaultChecked


                        Here is the example.



                        function bindFormChange($form) {

                        function touchButtons() {
                        var
                        changed_objects = ,
                        $observable_buttons = $form.find('input[type="submit"], button[type="submit"], button[data-object="reset-form"]');

                        changed_objects = $('input:text, input:checkbox, input:radio, textarea, select', $form).map(function () {
                        var
                        $input = $(this),
                        changed = false;

                        if ($input.is('input:text') || $input.is('textarea') ) {
                        changed = (($input).prop('defaultValue') != $input.val());
                        }
                        if (!changed && $input.is('select') ) {
                        changed = !$('option:selected', $input).prop('defaultSelected');
                        }
                        if (!changed && $input.is('input:checkbox') || $input.is('input:radio') ) {
                        changed = (($input).prop('defaultChecked') != $input.is(':checked'));
                        }
                        if (changed) {
                        return $input.attr('id');
                        }

                        }).toArray();

                        if (changed_objects.length) {
                        $observable_buttons.removeAttr('disabled')
                        } else {
                        $observable_buttons.attr('disabled', 'disabled');
                        }
                        };
                        touchButtons();

                        $('input, textarea, select', $form).each(function () {
                        var $input = $(this);

                        $input.on('keyup change', function () {
                        touchButtons();
                        });
                        });

                        };


                        Now just loop thru the forms on the page and you should see submit buttons disabled by default and they will be activated ONLY if you indeed will change some input value on the form.



                        $('form').each(function () {
                        bindFormChange($(this));
                        });


                        Implementation as a jQuery plugin is here https://github.com/kulbida/jmodifiable






                        share|improve this answer














                        Here is an elegant solution.



                        There is hidden property for each input element on the form that you can use to determine whether or not the value was changed.
                        Each type of input has it's own property name. For example




                        • for text/textarea it's defaultValue

                        • for select it's defaultSelect

                        • for checkbox/radio it's defaultChecked


                        Here is the example.



                        function bindFormChange($form) {

                        function touchButtons() {
                        var
                        changed_objects = ,
                        $observable_buttons = $form.find('input[type="submit"], button[type="submit"], button[data-object="reset-form"]');

                        changed_objects = $('input:text, input:checkbox, input:radio, textarea, select', $form).map(function () {
                        var
                        $input = $(this),
                        changed = false;

                        if ($input.is('input:text') || $input.is('textarea') ) {
                        changed = (($input).prop('defaultValue') != $input.val());
                        }
                        if (!changed && $input.is('select') ) {
                        changed = !$('option:selected', $input).prop('defaultSelected');
                        }
                        if (!changed && $input.is('input:checkbox') || $input.is('input:radio') ) {
                        changed = (($input).prop('defaultChecked') != $input.is(':checked'));
                        }
                        if (changed) {
                        return $input.attr('id');
                        }

                        }).toArray();

                        if (changed_objects.length) {
                        $observable_buttons.removeAttr('disabled')
                        } else {
                        $observable_buttons.attr('disabled', 'disabled');
                        }
                        };
                        touchButtons();

                        $('input, textarea, select', $form).each(function () {
                        var $input = $(this);

                        $input.on('keyup change', function () {
                        touchButtons();
                        });
                        });

                        };


                        Now just loop thru the forms on the page and you should see submit buttons disabled by default and they will be activated ONLY if you indeed will change some input value on the form.



                        $('form').each(function () {
                        bindFormChange($(this));
                        });


                        Implementation as a jQuery plugin is here https://github.com/kulbida/jmodifiable







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Apr 16 '15 at 21:32

























                        answered Apr 16 '15 at 19:36









                        Developer

                        7921011




                        7921011






















                            up vote
                            2
                            down vote













                            var formStr = JSON.stringify($("#form").serializeArray());
                            ...
                            function Submit(){
                            var newformStr = JSON.stringify($("#form").serializeArray());
                            if (formStr != newformStr){
                            ...
                            formChangedfunct();
                            ...
                            }
                            else {
                            ...
                            formUnchangedfunct();
                            ...
                            }
                            }





                            share|improve this answer





















                            • Yeah all these years later I don't even use jQuery anymore. Observers and bindings all the way now! Nice though for anyone using jQuery.
                              – mike
                              Jan 22 at 15:36

















                            up vote
                            2
                            down vote













                            var formStr = JSON.stringify($("#form").serializeArray());
                            ...
                            function Submit(){
                            var newformStr = JSON.stringify($("#form").serializeArray());
                            if (formStr != newformStr){
                            ...
                            formChangedfunct();
                            ...
                            }
                            else {
                            ...
                            formUnchangedfunct();
                            ...
                            }
                            }





                            share|improve this answer





















                            • Yeah all these years later I don't even use jQuery anymore. Observers and bindings all the way now! Nice though for anyone using jQuery.
                              – mike
                              Jan 22 at 15:36















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            var formStr = JSON.stringify($("#form").serializeArray());
                            ...
                            function Submit(){
                            var newformStr = JSON.stringify($("#form").serializeArray());
                            if (formStr != newformStr){
                            ...
                            formChangedfunct();
                            ...
                            }
                            else {
                            ...
                            formUnchangedfunct();
                            ...
                            }
                            }





                            share|improve this answer












                            var formStr = JSON.stringify($("#form").serializeArray());
                            ...
                            function Submit(){
                            var newformStr = JSON.stringify($("#form").serializeArray());
                            if (formStr != newformStr){
                            ...
                            formChangedfunct();
                            ...
                            }
                            else {
                            ...
                            formUnchangedfunct();
                            ...
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 11 '17 at 14:03









                            Péter Tajti

                            211




                            211












                            • Yeah all these years later I don't even use jQuery anymore. Observers and bindings all the way now! Nice though for anyone using jQuery.
                              – mike
                              Jan 22 at 15:36




















                            • Yeah all these years later I don't even use jQuery anymore. Observers and bindings all the way now! Nice though for anyone using jQuery.
                              – mike
                              Jan 22 at 15:36


















                            Yeah all these years later I don't even use jQuery anymore. Observers and bindings all the way now! Nice though for anyone using jQuery.
                            – mike
                            Jan 22 at 15:36






                            Yeah all these years later I don't even use jQuery anymore. Observers and bindings all the way now! Nice though for anyone using jQuery.
                            – mike
                            Jan 22 at 15:36












                            up vote
                            1
                            down vote













                            You need jQuery Form Observe plugin. That's what you are looking for.






                            share|improve this answer

























                              up vote
                              1
                              down vote













                              You need jQuery Form Observe plugin. That's what you are looking for.






                              share|improve this answer























                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                You need jQuery Form Observe plugin. That's what you are looking for.






                                share|improve this answer












                                You need jQuery Form Observe plugin. That's what you are looking for.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jun 11 '10 at 19:06









                                Sarfraz

                                295k60465544




                                295k60465544






















                                    up vote
                                    1
                                    down vote













                                    Extending Udi's answer, this only checks on form submission, not on every input change.



                                    $(document).ready( function () {
                                    var form_data = $('#myform').serialize();
                                    $('#myform').submit(function () {
                                    if ( form_data == $(this).serialize() ) {
                                    alert('no change');
                                    } else {
                                    alert('change');
                                    }
                                    });
                                    });





                                    share|improve this answer

























                                      up vote
                                      1
                                      down vote













                                      Extending Udi's answer, this only checks on form submission, not on every input change.



                                      $(document).ready( function () {
                                      var form_data = $('#myform').serialize();
                                      $('#myform').submit(function () {
                                      if ( form_data == $(this).serialize() ) {
                                      alert('no change');
                                      } else {
                                      alert('change');
                                      }
                                      });
                                      });





                                      share|improve this answer























                                        up vote
                                        1
                                        down vote










                                        up vote
                                        1
                                        down vote









                                        Extending Udi's answer, this only checks on form submission, not on every input change.



                                        $(document).ready( function () {
                                        var form_data = $('#myform').serialize();
                                        $('#myform').submit(function () {
                                        if ( form_data == $(this).serialize() ) {
                                        alert('no change');
                                        } else {
                                        alert('change');
                                        }
                                        });
                                        });





                                        share|improve this answer












                                        Extending Udi's answer, this only checks on form submission, not on every input change.



                                        $(document).ready( function () {
                                        var form_data = $('#myform').serialize();
                                        $('#myform').submit(function () {
                                        if ( form_data == $(this).serialize() ) {
                                        alert('no change');
                                        } else {
                                        alert('change');
                                        }
                                        });
                                        });






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Aug 13 '14 at 6:24









                                        Ruby Racer

                                        4,76111736




                                        4,76111736






















                                            up vote
                                            0
                                            down vote













                                            First, I'd add a hidden input to your form to track the state of the form. Then, I'd use this jQuery snippet to set the value of the hidden input when something on the form changes:



                                                $("form")
                                            .find("input")
                                            .change(function(){
                                            if ($("#hdnFormChanged").val() == "no")
                                            {
                                            $("#hdnFormChanged").val("yes");
                                            }
                                            });


                                            When your button is clicked, you can check the state of your hidden input:



                                            $("#Button").click(function(){
                                            if($("#hdnFormChanged").val() == "yes")
                                            {
                                            // handler code here...
                                            }
                                            });





                                            share|improve this answer



















                                            • 6




                                              No reason for extra inputs :) Check out .data()
                                              – Nick Craver
                                              Jun 11 '10 at 19:10










                                            • I learn something new every day!
                                              – JMP
                                              Jun 11 '10 at 19:41










                                            • -1 totally agree with Nick Crave.
                                              – Anonymous
                                              Sep 7 '12 at 19:02















                                            up vote
                                            0
                                            down vote













                                            First, I'd add a hidden input to your form to track the state of the form. Then, I'd use this jQuery snippet to set the value of the hidden input when something on the form changes:



                                                $("form")
                                            .find("input")
                                            .change(function(){
                                            if ($("#hdnFormChanged").val() == "no")
                                            {
                                            $("#hdnFormChanged").val("yes");
                                            }
                                            });


                                            When your button is clicked, you can check the state of your hidden input:



                                            $("#Button").click(function(){
                                            if($("#hdnFormChanged").val() == "yes")
                                            {
                                            // handler code here...
                                            }
                                            });





                                            share|improve this answer



















                                            • 6




                                              No reason for extra inputs :) Check out .data()
                                              – Nick Craver
                                              Jun 11 '10 at 19:10










                                            • I learn something new every day!
                                              – JMP
                                              Jun 11 '10 at 19:41










                                            • -1 totally agree with Nick Crave.
                                              – Anonymous
                                              Sep 7 '12 at 19:02













                                            up vote
                                            0
                                            down vote










                                            up vote
                                            0
                                            down vote









                                            First, I'd add a hidden input to your form to track the state of the form. Then, I'd use this jQuery snippet to set the value of the hidden input when something on the form changes:



                                                $("form")
                                            .find("input")
                                            .change(function(){
                                            if ($("#hdnFormChanged").val() == "no")
                                            {
                                            $("#hdnFormChanged").val("yes");
                                            }
                                            });


                                            When your button is clicked, you can check the state of your hidden input:



                                            $("#Button").click(function(){
                                            if($("#hdnFormChanged").val() == "yes")
                                            {
                                            // handler code here...
                                            }
                                            });





                                            share|improve this answer














                                            First, I'd add a hidden input to your form to track the state of the form. Then, I'd use this jQuery snippet to set the value of the hidden input when something on the form changes:



                                                $("form")
                                            .find("input")
                                            .change(function(){
                                            if ($("#hdnFormChanged").val() == "no")
                                            {
                                            $("#hdnFormChanged").val("yes");
                                            }
                                            });


                                            When your button is clicked, you can check the state of your hidden input:



                                            $("#Button").click(function(){
                                            if($("#hdnFormChanged").val() == "yes")
                                            {
                                            // handler code here...
                                            }
                                            });






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Jun 11 '10 at 19:08

























                                            answered Jun 11 '10 at 18:51









                                            JMP

                                            6,07842433




                                            6,07842433








                                            • 6




                                              No reason for extra inputs :) Check out .data()
                                              – Nick Craver
                                              Jun 11 '10 at 19:10










                                            • I learn something new every day!
                                              – JMP
                                              Jun 11 '10 at 19:41










                                            • -1 totally agree with Nick Crave.
                                              – Anonymous
                                              Sep 7 '12 at 19:02














                                            • 6




                                              No reason for extra inputs :) Check out .data()
                                              – Nick Craver
                                              Jun 11 '10 at 19:10










                                            • I learn something new every day!
                                              – JMP
                                              Jun 11 '10 at 19:41










                                            • -1 totally agree with Nick Crave.
                                              – Anonymous
                                              Sep 7 '12 at 19:02








                                            6




                                            6




                                            No reason for extra inputs :) Check out .data()
                                            – Nick Craver
                                            Jun 11 '10 at 19:10




                                            No reason for extra inputs :) Check out .data()
                                            – Nick Craver
                                            Jun 11 '10 at 19:10












                                            I learn something new every day!
                                            – JMP
                                            Jun 11 '10 at 19:41




                                            I learn something new every day!
                                            – JMP
                                            Jun 11 '10 at 19:41












                                            -1 totally agree with Nick Crave.
                                            – Anonymous
                                            Sep 7 '12 at 19:02




                                            -1 totally agree with Nick Crave.
                                            – Anonymous
                                            Sep 7 '12 at 19:02










                                            up vote
                                            0
                                            down vote
















                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            $("#result").html($(this).val());
                                            });

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                            <h2>Form "your_form_name"</h2>
                                            <form name="your_form_name">
                                            <input type="text" name="one_a" id="one_a" value="AAAAAAAA" />
                                            <input type="text" name="one_b" id="one_b" value="BBBBBBBB" />
                                            <input type="text" name="one_c" id="one_c" value="CCCCCCCC" />
                                            <select name="one_d">
                                            <option value="111111">111111</option>
                                            <option value="222222">222222</option>
                                            <option value="333333">333333</option>
                                            </select>
                                            </form>
                                            <hr/>
                                            <h2>Form "your_other_form_name"</h2>
                                            <form name="your_other_form_name">
                                            <input type="text" name="two_a" id="two_a" value="DDDDDDDD" />
                                            <input type="text" name="two_b" id="two_b" value="EEEEEEEE" />
                                            <input type="text" name="two_c" id="two_c" value="FFFFFFFF" />
                                            <input type="text" name="two_d" id="two_d" value="GGGGGGGG" />
                                            <input type="text" name="two_e" id="two_f" value="HHHHHHHH" />
                                            <input type="text" name="two_f" id="two_e" value="IIIIIIII" />
                                            <select name="two_g">
                                            <option value="444444">444444</option>
                                            <option value="555555">555555</option>
                                            <option value="666666">666666</option>
                                            </select>
                                            </form>
                                            <h2>Result</h2>
                                            <div id="result">
                                            <h2>Click on a field..</h2>
                                            </div>





                                            In addition to above @JoeD's answer.



                                            If you want to target fields in a particular form (assuming there are more than one forms) than just fields, you can use the following code:



                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            // A form element was clicked
                                            });





                                            share|improve this answer























                                            • Just FYI, this probably won't do what you want - it targets the input elements of the "your_form_name" form, and all selects in the document. Also, for performance reasons, it is always better to use $.fn.find rather than just targeting descendent selectors, e.g. $('form').find('input,select')
                                              – dgo
                                              Jan 29 at 20:35










                                            • @user1167442 How about if we have more than one form? My example is for a situation when there's more than one form in a single page.
                                              – Anjana Silva
                                              Jan 30 at 8:49






                                            • 1




                                              But still - your example will find all the input elements that are children of the "your_form_name" form, and then find all selects whether they are children of "your_form_name" or any form at all. Your example won't work for multiple forms, unless you are only targeting the selects of those forms. Also, if you are referring to the performance piece, it is always better to use $.fn.find - faster than even $.fn.filter - to reference children rather than using descendent selectors. vaughnroyko.com/the-real-scoop-on-jquery-find-performance
                                              – dgo
                                              Jan 30 at 18:02






                                            • 1




                                              @user1167442, you are absolutely right. I missed the fact that select is being targeted regardless of what form you are in. I have corrected that. Thank you very much for pointing out. Cheers!
                                              – Anjana Silva
                                              Jan 31 at 9:42















                                            up vote
                                            0
                                            down vote
















                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            $("#result").html($(this).val());
                                            });

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                            <h2>Form "your_form_name"</h2>
                                            <form name="your_form_name">
                                            <input type="text" name="one_a" id="one_a" value="AAAAAAAA" />
                                            <input type="text" name="one_b" id="one_b" value="BBBBBBBB" />
                                            <input type="text" name="one_c" id="one_c" value="CCCCCCCC" />
                                            <select name="one_d">
                                            <option value="111111">111111</option>
                                            <option value="222222">222222</option>
                                            <option value="333333">333333</option>
                                            </select>
                                            </form>
                                            <hr/>
                                            <h2>Form "your_other_form_name"</h2>
                                            <form name="your_other_form_name">
                                            <input type="text" name="two_a" id="two_a" value="DDDDDDDD" />
                                            <input type="text" name="two_b" id="two_b" value="EEEEEEEE" />
                                            <input type="text" name="two_c" id="two_c" value="FFFFFFFF" />
                                            <input type="text" name="two_d" id="two_d" value="GGGGGGGG" />
                                            <input type="text" name="two_e" id="two_f" value="HHHHHHHH" />
                                            <input type="text" name="two_f" id="two_e" value="IIIIIIII" />
                                            <select name="two_g">
                                            <option value="444444">444444</option>
                                            <option value="555555">555555</option>
                                            <option value="666666">666666</option>
                                            </select>
                                            </form>
                                            <h2>Result</h2>
                                            <div id="result">
                                            <h2>Click on a field..</h2>
                                            </div>





                                            In addition to above @JoeD's answer.



                                            If you want to target fields in a particular form (assuming there are more than one forms) than just fields, you can use the following code:



                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            // A form element was clicked
                                            });





                                            share|improve this answer























                                            • Just FYI, this probably won't do what you want - it targets the input elements of the "your_form_name" form, and all selects in the document. Also, for performance reasons, it is always better to use $.fn.find rather than just targeting descendent selectors, e.g. $('form').find('input,select')
                                              – dgo
                                              Jan 29 at 20:35










                                            • @user1167442 How about if we have more than one form? My example is for a situation when there's more than one form in a single page.
                                              – Anjana Silva
                                              Jan 30 at 8:49






                                            • 1




                                              But still - your example will find all the input elements that are children of the "your_form_name" form, and then find all selects whether they are children of "your_form_name" or any form at all. Your example won't work for multiple forms, unless you are only targeting the selects of those forms. Also, if you are referring to the performance piece, it is always better to use $.fn.find - faster than even $.fn.filter - to reference children rather than using descendent selectors. vaughnroyko.com/the-real-scoop-on-jquery-find-performance
                                              – dgo
                                              Jan 30 at 18:02






                                            • 1




                                              @user1167442, you are absolutely right. I missed the fact that select is being targeted regardless of what form you are in. I have corrected that. Thank you very much for pointing out. Cheers!
                                              – Anjana Silva
                                              Jan 31 at 9:42













                                            up vote
                                            0
                                            down vote










                                            up vote
                                            0
                                            down vote












                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            $("#result").html($(this).val());
                                            });

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                            <h2>Form "your_form_name"</h2>
                                            <form name="your_form_name">
                                            <input type="text" name="one_a" id="one_a" value="AAAAAAAA" />
                                            <input type="text" name="one_b" id="one_b" value="BBBBBBBB" />
                                            <input type="text" name="one_c" id="one_c" value="CCCCCCCC" />
                                            <select name="one_d">
                                            <option value="111111">111111</option>
                                            <option value="222222">222222</option>
                                            <option value="333333">333333</option>
                                            </select>
                                            </form>
                                            <hr/>
                                            <h2>Form "your_other_form_name"</h2>
                                            <form name="your_other_form_name">
                                            <input type="text" name="two_a" id="two_a" value="DDDDDDDD" />
                                            <input type="text" name="two_b" id="two_b" value="EEEEEEEE" />
                                            <input type="text" name="two_c" id="two_c" value="FFFFFFFF" />
                                            <input type="text" name="two_d" id="two_d" value="GGGGGGGG" />
                                            <input type="text" name="two_e" id="two_f" value="HHHHHHHH" />
                                            <input type="text" name="two_f" id="two_e" value="IIIIIIII" />
                                            <select name="two_g">
                                            <option value="444444">444444</option>
                                            <option value="555555">555555</option>
                                            <option value="666666">666666</option>
                                            </select>
                                            </form>
                                            <h2>Result</h2>
                                            <div id="result">
                                            <h2>Click on a field..</h2>
                                            </div>





                                            In addition to above @JoeD's answer.



                                            If you want to target fields in a particular form (assuming there are more than one forms) than just fields, you can use the following code:



                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            // A form element was clicked
                                            });





                                            share|improve this answer

















                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            $("#result").html($(this).val());
                                            });

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                            <h2>Form "your_form_name"</h2>
                                            <form name="your_form_name">
                                            <input type="text" name="one_a" id="one_a" value="AAAAAAAA" />
                                            <input type="text" name="one_b" id="one_b" value="BBBBBBBB" />
                                            <input type="text" name="one_c" id="one_c" value="CCCCCCCC" />
                                            <select name="one_d">
                                            <option value="111111">111111</option>
                                            <option value="222222">222222</option>
                                            <option value="333333">333333</option>
                                            </select>
                                            </form>
                                            <hr/>
                                            <h2>Form "your_other_form_name"</h2>
                                            <form name="your_other_form_name">
                                            <input type="text" name="two_a" id="two_a" value="DDDDDDDD" />
                                            <input type="text" name="two_b" id="two_b" value="EEEEEEEE" />
                                            <input type="text" name="two_c" id="two_c" value="FFFFFFFF" />
                                            <input type="text" name="two_d" id="two_d" value="GGGGGGGG" />
                                            <input type="text" name="two_e" id="two_f" value="HHHHHHHH" />
                                            <input type="text" name="two_f" id="two_e" value="IIIIIIII" />
                                            <select name="two_g">
                                            <option value="444444">444444</option>
                                            <option value="555555">555555</option>
                                            <option value="666666">666666</option>
                                            </select>
                                            </form>
                                            <h2>Result</h2>
                                            <div id="result">
                                            <h2>Click on a field..</h2>
                                            </div>





                                            In addition to above @JoeD's answer.



                                            If you want to target fields in a particular form (assuming there are more than one forms) than just fields, you can use the following code:



                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            // A form element was clicked
                                            });





                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            $("#result").html($(this).val());
                                            });

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                            <h2>Form "your_form_name"</h2>
                                            <form name="your_form_name">
                                            <input type="text" name="one_a" id="one_a" value="AAAAAAAA" />
                                            <input type="text" name="one_b" id="one_b" value="BBBBBBBB" />
                                            <input type="text" name="one_c" id="one_c" value="CCCCCCCC" />
                                            <select name="one_d">
                                            <option value="111111">111111</option>
                                            <option value="222222">222222</option>
                                            <option value="333333">333333</option>
                                            </select>
                                            </form>
                                            <hr/>
                                            <h2>Form "your_other_form_name"</h2>
                                            <form name="your_other_form_name">
                                            <input type="text" name="two_a" id="two_a" value="DDDDDDDD" />
                                            <input type="text" name="two_b" id="two_b" value="EEEEEEEE" />
                                            <input type="text" name="two_c" id="two_c" value="FFFFFFFF" />
                                            <input type="text" name="two_d" id="two_d" value="GGGGGGGG" />
                                            <input type="text" name="two_e" id="two_f" value="HHHHHHHH" />
                                            <input type="text" name="two_f" id="two_e" value="IIIIIIII" />
                                            <select name="two_g">
                                            <option value="444444">444444</option>
                                            <option value="555555">555555</option>
                                            <option value="666666">666666</option>
                                            </select>
                                            </form>
                                            <h2>Result</h2>
                                            <div id="result">
                                            <h2>Click on a field..</h2>
                                            </div>





                                            $('form[name="your_form_name"] input, form[name="your_form_name"] select').click(function() {
                                            $("#result").html($(this).val());
                                            });

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                            <h2>Form "your_form_name"</h2>
                                            <form name="your_form_name">
                                            <input type="text" name="one_a" id="one_a" value="AAAAAAAA" />
                                            <input type="text" name="one_b" id="one_b" value="BBBBBBBB" />
                                            <input type="text" name="one_c" id="one_c" value="CCCCCCCC" />
                                            <select name="one_d">
                                            <option value="111111">111111</option>
                                            <option value="222222">222222</option>
                                            <option value="333333">333333</option>
                                            </select>
                                            </form>
                                            <hr/>
                                            <h2>Form "your_other_form_name"</h2>
                                            <form name="your_other_form_name">
                                            <input type="text" name="two_a" id="two_a" value="DDDDDDDD" />
                                            <input type="text" name="two_b" id="two_b" value="EEEEEEEE" />
                                            <input type="text" name="two_c" id="two_c" value="FFFFFFFF" />
                                            <input type="text" name="two_d" id="two_d" value="GGGGGGGG" />
                                            <input type="text" name="two_e" id="two_f" value="HHHHHHHH" />
                                            <input type="text" name="two_f" id="two_e" value="IIIIIIII" />
                                            <select name="two_g">
                                            <option value="444444">444444</option>
                                            <option value="555555">555555</option>
                                            <option value="666666">666666</option>
                                            </select>
                                            </form>
                                            <h2>Result</h2>
                                            <div id="result">
                                            <h2>Click on a field..</h2>
                                            </div>






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Jan 31 at 9:41

























                                            answered May 19 '17 at 12:11









                                            Anjana Silva

                                            2,04222130




                                            2,04222130












                                            • Just FYI, this probably won't do what you want - it targets the input elements of the "your_form_name" form, and all selects in the document. Also, for performance reasons, it is always better to use $.fn.find rather than just targeting descendent selectors, e.g. $('form').find('input,select')
                                              – dgo
                                              Jan 29 at 20:35










                                            • @user1167442 How about if we have more than one form? My example is for a situation when there's more than one form in a single page.
                                              – Anjana Silva
                                              Jan 30 at 8:49






                                            • 1




                                              But still - your example will find all the input elements that are children of the "your_form_name" form, and then find all selects whether they are children of "your_form_name" or any form at all. Your example won't work for multiple forms, unless you are only targeting the selects of those forms. Also, if you are referring to the performance piece, it is always better to use $.fn.find - faster than even $.fn.filter - to reference children rather than using descendent selectors. vaughnroyko.com/the-real-scoop-on-jquery-find-performance
                                              – dgo
                                              Jan 30 at 18:02






                                            • 1




                                              @user1167442, you are absolutely right. I missed the fact that select is being targeted regardless of what form you are in. I have corrected that. Thank you very much for pointing out. Cheers!
                                              – Anjana Silva
                                              Jan 31 at 9:42


















                                            • Just FYI, this probably won't do what you want - it targets the input elements of the "your_form_name" form, and all selects in the document. Also, for performance reasons, it is always better to use $.fn.find rather than just targeting descendent selectors, e.g. $('form').find('input,select')
                                              – dgo
                                              Jan 29 at 20:35










                                            • @user1167442 How about if we have more than one form? My example is for a situation when there's more than one form in a single page.
                                              – Anjana Silva
                                              Jan 30 at 8:49






                                            • 1




                                              But still - your example will find all the input elements that are children of the "your_form_name" form, and then find all selects whether they are children of "your_form_name" or any form at all. Your example won't work for multiple forms, unless you are only targeting the selects of those forms. Also, if you are referring to the performance piece, it is always better to use $.fn.find - faster than even $.fn.filter - to reference children rather than using descendent selectors. vaughnroyko.com/the-real-scoop-on-jquery-find-performance
                                              – dgo
                                              Jan 30 at 18:02






                                            • 1




                                              @user1167442, you are absolutely right. I missed the fact that select is being targeted regardless of what form you are in. I have corrected that. Thank you very much for pointing out. Cheers!
                                              – Anjana Silva
                                              Jan 31 at 9:42
















                                            Just FYI, this probably won't do what you want - it targets the input elements of the "your_form_name" form, and all selects in the document. Also, for performance reasons, it is always better to use $.fn.find rather than just targeting descendent selectors, e.g. $('form').find('input,select')
                                            – dgo
                                            Jan 29 at 20:35




                                            Just FYI, this probably won't do what you want - it targets the input elements of the "your_form_name" form, and all selects in the document. Also, for performance reasons, it is always better to use $.fn.find rather than just targeting descendent selectors, e.g. $('form').find('input,select')
                                            – dgo
                                            Jan 29 at 20:35












                                            @user1167442 How about if we have more than one form? My example is for a situation when there's more than one form in a single page.
                                            – Anjana Silva
                                            Jan 30 at 8:49




                                            @user1167442 How about if we have more than one form? My example is for a situation when there's more than one form in a single page.
                                            – Anjana Silva
                                            Jan 30 at 8:49




                                            1




                                            1




                                            But still - your example will find all the input elements that are children of the "your_form_name" form, and then find all selects whether they are children of "your_form_name" or any form at all. Your example won't work for multiple forms, unless you are only targeting the selects of those forms. Also, if you are referring to the performance piece, it is always better to use $.fn.find - faster than even $.fn.filter - to reference children rather than using descendent selectors. vaughnroyko.com/the-real-scoop-on-jquery-find-performance
                                            – dgo
                                            Jan 30 at 18:02




                                            But still - your example will find all the input elements that are children of the "your_form_name" form, and then find all selects whether they are children of "your_form_name" or any form at all. Your example won't work for multiple forms, unless you are only targeting the selects of those forms. Also, if you are referring to the performance piece, it is always better to use $.fn.find - faster than even $.fn.filter - to reference children rather than using descendent selectors. vaughnroyko.com/the-real-scoop-on-jquery-find-performance
                                            – dgo
                                            Jan 30 at 18:02




                                            1




                                            1




                                            @user1167442, you are absolutely right. I missed the fact that select is being targeted regardless of what form you are in. I have corrected that. Thank you very much for pointing out. Cheers!
                                            – Anjana Silva
                                            Jan 31 at 9:42




                                            @user1167442, you are absolutely right. I missed the fact that select is being targeted regardless of what form you are in. I have corrected that. Thank you very much for pointing out. Cheers!
                                            – Anjana Silva
                                            Jan 31 at 9:42










                                            up vote
                                            0
                                            down vote













                                            Try this:



                                            <script>
                                            var form_original_data = $("form").serialize();
                                            var form_submit=false;
                                            $('[type="submit"]').click(function() {
                                            form_submit=true;
                                            });
                                            window.onbeforeunload = function() {
                                            //console.log($("form").submit());
                                            if ($("form").serialize() != form_original_data && form_submit==false) {
                                            return "Do you really want to leave without saving?";
                                            }
                                            };
                                            </script>





                                            share|improve this answer



























                                              up vote
                                              0
                                              down vote













                                              Try this:



                                              <script>
                                              var form_original_data = $("form").serialize();
                                              var form_submit=false;
                                              $('[type="submit"]').click(function() {
                                              form_submit=true;
                                              });
                                              window.onbeforeunload = function() {
                                              //console.log($("form").submit());
                                              if ($("form").serialize() != form_original_data && form_submit==false) {
                                              return "Do you really want to leave without saving?";
                                              }
                                              };
                                              </script>





                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                Try this:



                                                <script>
                                                var form_original_data = $("form").serialize();
                                                var form_submit=false;
                                                $('[type="submit"]').click(function() {
                                                form_submit=true;
                                                });
                                                window.onbeforeunload = function() {
                                                //console.log($("form").submit());
                                                if ($("form").serialize() != form_original_data && form_submit==false) {
                                                return "Do you really want to leave without saving?";
                                                }
                                                };
                                                </script>





                                                share|improve this answer














                                                Try this:



                                                <script>
                                                var form_original_data = $("form").serialize();
                                                var form_submit=false;
                                                $('[type="submit"]').click(function() {
                                                form_submit=true;
                                                });
                                                window.onbeforeunload = function() {
                                                //console.log($("form").submit());
                                                if ($("form").serialize() != form_original_data && form_submit==false) {
                                                return "Do you really want to leave without saving?";
                                                }
                                                };
                                                </script>






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited 2 days ago









                                                tiagoperes

                                                2,23821432




                                                2,23821432










                                                answered 2 days ago









                                                Vinod saraswat

                                                74




                                                74






























                                                     

                                                    draft saved


                                                    draft discarded



















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3025396%2fhow-do-you-handle-a-form-change-in-jquery%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Tonle Sap (See)

                                                    I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                                                    Guatemaltekische Davis-Cup-Mannschaft