I am encountering a problem about how to get the Dropdownlist text and value using Javascript from client side. I am using C# in Visual Studio 2005 and Javasript . The example codes are like:
( In this example code, I would like to access the text and value of the DropDownListReports in the run() function of Javascript. How can I do that? Any ideas? Thanks a lot. )
whuili_2006
Member
21 Points
45 Posts
Get the Dropdownlist text and value using Javascript from client side
Feb 05, 2007 08:40 PM|LINK
Hi,
I am encountering a problem about how to get the Dropdownlist text and value using Javascript from client side. I am using C# in Visual Studio 2005 and Javasript . The example codes are like:
( In this example code, I would like to access the text and value of the DropDownListReports in the run() function of Javascript. How can I do that? Any ideas? Thanks a lot. )
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownListReports.Items.Clear();
ListItem
listItem = new ListItem();listItem.Text = "aText";
listItem.Value = "aValue;
DropDownListReports.Items.Add(listItem);
}
}
on the its source page:
<
asp:DropDownList ID="DropDownListReports" Width = '300' runat="server" > </asp:DropDownList><input id="btnSubmit" type="button" value="Click to view report on another browser" onclick="run()" TabIndex="3" />
<
script type="text/javascript">function
run(){
// ?????? for the example, please just using alert to show the pair of text and value for this dropdownlist. Like: alert(dropdownlist.text);
}
Haissam
All-Star
37421 Points
5632 Posts
Re: Get the Dropdownlist text and value using Javascript from client side
Feb 05, 2007 09:50 PM|LINK
add the below code into your javascript function
alert(document.getElementById("<%=DropDownListReports.ClientID%>").options[document.getElementById("<%=DropDownListReports.ClientID%>").selectedIndex].text)
HC
------
Mark it as answered if it helps you
MCAD.NET
| Blog |
Rajganesh
Participant
1117 Points
188 Posts
Re: Get the Dropdownlist text and value using Javascript from client side
Feb 06, 2007 08:58 AM|LINK
__________________________________________________
My Blog
If this post answers your question please mark it as Answered.
hallic7
Member
10 Points
5 Posts
Re: Get the Dropdownlist text and value using Javascript from client side
Sep 10, 2010 07:08 PM|LINK
Hi Rajganesh I tried the solution you posted, but it's giving the following error in the IE 8 JScript Debugger...
'options' is null or not an object
Any solutions?
sushanth009
Contributor
6243 Points
1168 Posts
Re: Get the Dropdownlist text and value using Javascript from client side
Jul 11, 2012 05:54 AM|LINK
You can try using JQuery .. Try this code and see if it works
<asp:DropDownList ID="DropDownListReports" Width = '300' runat="server" > </asp:DropDownList> <input id="btnSubmit" type="button" value="Click to view report on another browser" TabIndex="3" /> <script type="text/javascript"> $(function() { $('#btnSubmit').on('click' , function(){ run(); }); }); function run() { // Selected value alert('Selected Value is : ' + $('[id*=DropDownListReports]').val()); // Selected Text alert('Selected Value is : ' + $('[id*=DropDownListReports] option:selected').text()); }