davidcahan
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. selectedIn dex].value ;
and heres my dropdownlist control:
<asp:DropDownList ID="ddlCountryID" onChange="SelectStates(thi s)" runat="server"></asp:DropD ownList>
but everytime i change the selection i get this error:
Error: 'ct100_ContentPlaceHolder1 _ddlCountr yID' 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.
Here's my Javascript code:
var dropdown = document.getElementById(<%
var text = dropdown.options[dropdown.
and heres my dropdownlist control:
<asp:DropDownList ID="ddlCountryID" onChange="SelectStates(thi
but everytime i change the selection i get this error:
Error: 'ct100_ContentPlaceHolder1
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.
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('< %=ddlCount ryID.Clien tID%>');
var dropdown = document.getElementById('<
ASKER
yea, it is ddlCountryID.ClientID. I pasted my code from an earlier undo.
ASKER
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
var ddlist=document.getElement ById('<%=d dlCountryI D.ClientID %>');
var val = ddlist.options[ddlist.sele ctedIndex] .value;
var text = ddlist.options[ddlist.sele ctedIndex] .text;
Where ddlCountryID is the comboBox Name
var val = ddlist.options[ddlist.sele
var text = ddlist.options[ddlist.sele
Where ddlCountryID is the comboBox Name
<%= ddlCountryID.ClientID %>
since your dropdown is
<asp:DropDownList ID="ddlCountryID" onChange="SelectStates(thi