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 3300604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:40:38+00:00 2026-05-17T20:40:38+00:00

Clarification This is part of a script that checks if a user has changed

  • 0

Clarification

This is part of a script that checks if a user has changed and values in a form. If the user attempts to leave the page after changing a value they get alerted via onbeforeunload and are presented with the option to either leave the page or stay.

The tricky part is determining the changed state of a (multiple) select list…which is where this question applies. I just wanted to see if anyone could spot any potential problems with the way it is being done.

Someone mentioned that it may not make sense to always use the default value for the comparisons. However, it does make sense in this case. If the user changes a value and then proceeds to change it back to the original before navigating away from the page they probably don’t want a “You’ve change soemthing on the page, Leave or Stay?” alert popping up.


The code below is intended to check a select list (<select>) to see if the “selected” attributes are the same as the default “selected” attributes. It should work on multiple-select lists as well as single-option select lists.

The function IsSelectChanged' should returntrueif the selected option(s) are not the same as the default andfalse` if the selected option(s) are the same as the default.

The code:

<select>
    <option selected="selected">Opt 1</option>
    <option>Opt 2</option>
</select>
<script>
    // the code:
    function IsSelectChanged(select){
        var options = select.options,
            i = 0,
            l = options.length;
        // collect current selected and defaultSelected
        for (; i < l; i++) {
            var option = options[i];
            // if it was selected by default but now it is not
            if (option.defaultSelected && !option.selected) {
                return true;
            }
            // if it is selected now but it was not by default
            if (option.selected && !option.defaultSelected) {
                return true;
            }
        }
        return false;
    }

    // implementation:
    $("select").change(function(){
        doSomethingWithValue( IsSelectChanged(this) );
    });
</script>

The code should work both for select lists that allow multiple selections/initial-selections and the single-selection variants (as seen above).

Can anyone spot any potential bugs or inefficiencies here? Or know of a better way of doing this?

Thanks

  • 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-17T20:40:38+00:00Added an answer on May 17, 2026 at 8:40 pm

    Since you’re testing for false && true or true && false, you should just be able to use !== since the idea seems to be that you want to return true when they don’t match.

    if(option.defaultSelected !== option.selected) {
        return true;
    }
    

    Also, since the order of the loop doesn’t seem to matter here, you should be able to use a while loop, which should be a little more efficient.

    var options = select.options,
        l = options.length;
    while ( l-- ) {
       ...
    

    This way obviates the need for a comparison.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, this is quite a special topic so this may not be of interest
When a GUI is composed of several subcomponents that I treat as individual Views
This question is a follow up to my previous Java GC question: Java Garbage
EDIT Public health warning - this question includes a false assumption about undefined behaviour.
I have a set of vertices(called A) and I want to find all the
Possible Duplicate: Why are C character literals ints instead of chars? Why when using
Say I have something like the following: dest = \n.join( [line for line in
There must be a simple solution out there, I'm trying to use NHibernate Validator
I routinely have to look at byte [1024] and longer in the debugger (VS
At my company we're using git for a couple of months now (and we're

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.