Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7192937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:03:45+00:00 2026-05-28T20:03:45+00:00

If all fields in the form are dirty return true if not then return

  • 0

If all fields in the form are dirty return true if not then return false.
Here is my code.

function getHasChanges() {
    var hasChanges = false;

    $(":input:not(:button):not([type=hidden])").each(function () {
        if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue != this.value) {
            hasChanges = true;
            return false;             }
        else {
            if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked != this.checked) {
                hasChanges = true;
                return false;                 }
            else {
                if ((this.type == "select-one" || this.type == "select-multiple")) {
                    for (var x = 0; x < this.length; x++) {
                        if (this.options[x].selected != this.options[x].defaultSelected) {
                            hasChanges = true;
                            return false;
                        }
                    }
                }
            }
        }
    });

    return hasChanges;
}

I used this code and tried to manipulate it but I couldn’t figure it out.

My question is: How to check if all fields on the form are dirty?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:03:52+00:00Added an answer on May 28, 2026 at 8:03 pm

    You are doing it wrong i think. You are returning false everywhere so the output will be always false irrespective of whether it has a change or not. Even if you have hasChanges = true; you are returning false Change your code:

    function getHasChanges() {
        var hasChanges = false;
    
        $(":input:not(:button):not([type=hidden])").each(function () {
            if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue != this.value) {
                hasChanges = true;
             }
            else 
             {
                if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked != this.checked) {
                    hasChanges = true;
                 }
                else 
                {
                    if ((this.type == "select-one" || this.type == "select-multiple")) {
                        for (var x = 0; x < this.length; x++) {
                            if (this.options[x].selected != this.options[x].defaultSelected) {
                                hasChanges = true;
                           }
                        }
                    }
                }
            }
        });
    
        return hasChanges;
    }
    

    EDIT:

    Even the above code will have problem; because once we set it to true, even if the fields in the below query are not changed we are not setting it to false. You need to use this one:

    function getHasChanges() {
                var hasChanges = false;
    
                $(":input:not(:button):not([type=hidden])").each(function () {
                    if ((this.type == "text" || this.type == "textarea" || this.type == "hidden")) {
                        if(this.defaultValue != this.value) {
                            hasChanges = true;
                        } else {
                            hasChanges = false;
                        }
                        return hasChanges;
                     } else {
                        if ((this.type == "radio" || this.type == "checkbox")) {
                            if(this.defaultChecked != this.checked) {
                                hasChanges = true;
                            } else {
                                hasChanges = false;
                            }
                         }
                         return hasChanges;
                        else 
                        {
                            if ((this.type == "select-one" || this.type == "select-multiple")) {
                                for (var x = 0; x < this.length; x++) {
                                    if (this.options[x].selected != this.options[x].defaultSelected) {
                                        hasChanges = true;
                                    }
                                }
                            }
                            return hasChanges;
                        }
                    }
                });
    
                return hasChanges;
        }
    

    Hope this helps 🙂

    Missed a bracket. Please check this:

    function getHasChanges() {
        var hasChanges = false;
        $(":input:not(:button):not([type=hidden])").each(function () {
            if ((this.type == "text" || this.type == "textarea" || this.type == "hidden")) {
                if(this.defaultValue != this.value) {
                    hasChanges = true;
                } else {
                    hasChanges = false;
                }
                return hasChanges;
             } else if ((this.type == "radio" || this.type == "checkbox")) {
                    if(this.defaultChecked != this.checked) {
                        hasChanges = true;
                    } else {
                        hasChanges = false;
                    }
                 return hasChanges;
             } else if (this.type == "select-one" || this.type == "select-multiple") {
                    for (var x = 0; x < this.length; x++) {
                        if (this.options[x].selected != this.options[x].defaultSelected) {
                            hasChanges = true;
                        }
                    }
                    return hasChanges;
             }
        });
        return hasChanges;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function to edit all fields that come from the form and
HTML has an input button type to reset all fields in a form to
I have a form and one of the fields is a check-all-that-applies type field
I've created a form that stores free text fields into a MySQL database. All
I'm tring to grab all fields from the latest Cash record, and then all
I have a dialog form. <div id=dialog-form title=Create new Admin> <p class=validateTips>All form fields
I had a requirement to copy all fields from one form to another similar
I want to clear all input and textarea fields in a form. It works
How can we make all fields in a form read only ?
How to 'hide' all fields in a form based on a model class in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.