Link to home
Start Free TrialLog in
Avatar of davidcahan
davidcahanFlag for United States of America

asked on

Get Selected Value of DropDownList using Javascript

This shouldn't be hard

Here's my Javascript code:

var dropdown = document.getElementById(<%= ddlStateID.ClientID %>);
var text = dropdown.options[dropdown.selectedIndex].value;

and heres my dropdownlist control:

<asp:DropDownList ID="ddlCountryID" onChange="SelectStates(this)" runat="server"></asp:DropDownList>

but everytime i change the selection i get this error:
Error: 'ct100_ContentPlaceHolder1_ddlCountryID' is undefined.

I think it's just how i'm calling the options or something in the javascript...i've seen this before, fixed this before, but can't find the code where i did it.  Big deadline on monday and i'm going nuts.
Avatar of third
third
Flag of Philippines image

isn't this supposed to be

<%= ddlCountryID.ClientID %>

since your dropdown is

<asp:DropDownList ID="ddlCountryID" onChange="SelectStates(this)" runat="server"></asp:DropDownList>
another way is run your aspx page from a browser and check the assigned id of your dropdown. substitute that id to your javascript function.
also, I think you missed the single quotes.

var dropdown = document.getElementById('<%=ddlCountryID.ClientID%>');
Avatar of davidcahan

ASKER

yea, it is ddlCountryID.ClientID.  I pasted my code from an earlier undo.
I found my code to do it.  It's just a slight wrinkle from other syntax i've seen posted on other sites.  None of that stuff ever worked, but this does:


var text = document.getElementById('<%= ddlCountryID.ClientID %>').options[document.getElementById('<%= ddlCountryID.ClientID %>').selectedIndex].value;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of third
third
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll give ya the points cause you helped get me motivated enough to find my own code, plus your answer was in the right direction
Avatar of manojkumarps
manojkumarps

var ddlist=document.getElementById('<%=ddlCountryID.ClientID%>');
        var val = ddlist.options[ddlist.selectedIndex].value;
        var text = ddlist.options[ddlist.selectedIndex].text;

Where ddlCountryID is the comboBox Name