Code Review: Efficient? Will it work?
<h2>Clarification</h2>

<p>This is part of a script that checks if a user has <em>changed</em> 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.</p>

<p>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.</p>

<p>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.</p>

<hr>

<p>The code below is intended to check a select list (<code>&lt;select&gt;</code>) 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.</p>

<p>The function <code>IsSelectChanged' should return</code>true<code>if the selected option(s) are not the same as the default and</code>false` if the selected option(s) are the same as the default.</p>

<p>The code:</p>

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

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

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

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

<p>Thanks</p>
