Solved

Get the value and text of dropdown selected, using javascript

Posted on 2008-06-18
5
19,790 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

751 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