0

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:

<asp:DropDownList ID="ddlMyDropDownList" runat="server">
    <asp:ListItem> </asp:ListItem>
    <asp:ListItem>Yes</asp:ListItem>
    <asp:ListItem>No</asp:ListItem>
</asp:DropDownList>

In the code behind I'm adding the onChange attribute:

ddlMyDropDownList.Attributes.Add("onchange", "enableButton();")

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

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

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:

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

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?

2
  • 1
    Are you sure you are getting a reference to the control in ddl? If var ddl is null or undefined, the alert will still fire. Perhaps you could step into the code from a breakpoint there, or try examining a different property. Commented Feb 1, 2012 at 14:18
  • If you alert ddl, what do you get back? Commented Feb 1, 2012 at 14:18

2 Answers 2

2

If you alert(ddl) you will probably find that ddl is null because there is no element with an id of #<%= ddlMyDropDownList.ClientID %>. Try removing the # from the beginning of the id string.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice spot on the hash. Rather than returning null i suspect it'll return undefined.
Actually alert(ddl) showed "null", at least in FireFox. Thanks for the catch. Those are exactly the kinds of errors that I overlook too easily.
0

Throw it out of your JS-function:

<script type="text/javascript">
  var ddlMyDropDownList = '<%= ddlMyDropDownList.ClientID %>';

  function enableButton() {
    var ddl = document.getElementById(ddlMyDropDownList);
  }
</script>

Or - better when possible - pass the reference directly:

ddlMyDropDownList.Attributes.Add("onchange", "enableButton(this);")

Then you don't need to find the element:

function enableButton(ddl) {
    var idx = ddl.selectedIndex;  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.