Solved

Get the value and text of dropdown selected, using javascript

Posted on 2008-06-18
5
19,781 Views
Last Modified: 2012-08-13
Hi,
I have dropdown,

<asp:dropdownlist id="Lst_Town" runat="server" onchange="fillCountry(this.selectedIndex);" ></asp:dropdownlist>

The dropdown is assigned as follwoing,
DataTextField of dropdown is = "Name"
DataValueField of dropdown is = "Name_Id"


In the dropdown i call a javascript function fillCountry()

function fillCountry(selInd)
{
    // here i want to do something like this...
 alert(Lst_Town.Text);
 alert(Lst_Town.Value);
}

On the whole, when the user select the dropdown, i want to alert, to show the selected value and the selected text.

Urgent,

Thanks.
0
Comment
Question by:srk1982
  • 3
5 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21819683
Try,

strCountry=Lst_Town.SelectedItem.Value;
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21819689
function fillCountry(selInd)
{
    // here i want to do something like this...
 alert(Lst_Town.SelectedItem.Text);
 alert(Lst_Town.SelectedItem.Value);
}
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21819698
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 250 total points
ID: 21819719
Replace frmPreferences with your form name

function fillCountry(selInd)
{
    // here i want to do something like this...
      var w = document.frmPreferences.Lst_Town.options[document.frmPreferences.Lst_Town.selectedIndex].value;
var selected_text = document.frmPreferences.Lst_Town.options[document.frmPreferences.Lst_Town.selectedIndex].text;
alert(w);
alert(selected_text);
}
0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 28940522
Please check the example
//To get value
var myvalue = document.frm1.ddl.options[document.frm1.ddl.selectedIndex].value;
alert(myvalue);

//To get Text
var mytext = document.frm1.ddl.options[document.frm1.ddl.selectedIndex].text;
alert(mytext);

Open in new window

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
html input 8 43
jQuery Toggle & Anchor Links 5 43
Code works but breaks when I add one section 4 20
what is the best Integrated development environment 2 21
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question