Search code examples
jqueryjquery-selectors

How can I find input elements which have not been changed in jQuery?


I want to find all form elements, with a specific class, and check if they have been altered or not. The if-statement does its job, but I have no idea to correct the find thingy. Is this in the right direction or have I got it all wrong?

$("input").find(".formWithMyClass").load(function () {
    if (this.value !== this.defaultValue) {
        $(this).addClass("inactiveForm");
    }
});

Cheers!


Solution

  • You could write a custom selector...

    (function($) {
        $.expr[':'].defaultValue = function(obj) {
           return obj.value === obj.defaultValue;
        };
    })(jQuery);
    

    Then you should be able to do $('input:defaultValue') to get all input elements of whom their value property is set to defaultValue.

    However, if you want to find all input type elements, you may want to extend that custom selector.

    I can't seem to find a way to get select element's original values (not without setting them up first and foremost). Their defaultValue property is undefined and selectedIndex could be the last chosen value.