Javascript dropdownlist.selectedIndex causing error in ASP.net page
<p>I'm trying to simply enable/disable a button based on whether a value has been selected from a dropdownlist, and for some reason javascript isn't getting the selected index of the dropdownlist. Here's the declaration of the drop down list:</p>

<pre><code>&lt;asp:DropDownList ID="ddlMyDropDownList" runat="server"&gt;
    &lt;asp:ListItem&gt; &lt;/asp:ListItem&gt;
    &lt;asp:ListItem&gt;Yes&lt;/asp:ListItem&gt;
    &lt;asp:ListItem&gt;No&lt;/asp:ListItem&gt;
&lt;/asp:DropDownList&gt;
</code></pre>

<p>In the code behind I'm adding the onChange attribute:</p>

<pre><code>ddlMyDropDownList.Attributes.Add("onchange", "enableButton();")
</code></pre>

<p>And this is what the enableButton function looks like, stripped down to the part that is causing problems:</p>

<pre><code>function enableButton() {
    var ddl = document.getElementById('#&lt;%= ddlMyDropDownList.ClientID %&gt;');
    alert("Testing");
    var idx = ddl.selectedIndex;
};
</code></pre>

<p>As written the alert fires whenever I change the value of the drop down list. If I move the alert to after the idx line, however:</p>

<pre><code>function enableButton() {
    var ddl = document.getElementById('#&lt;%= ddlMyDropDownList.ClientID %&gt;');
    var idx = ddl.selectedIndex;
    alert("Testing");
};
</code></pre>

<p>it doesn't fire, indicating that the ddl.selectedIndex is causing an error somehow. Why might this be occurring and how do I remedy it? Assuming I want to test only if the user has selected anything other than the first option, is there a better/easier way to do so?</p>
