Solved

Get the value and text of dropdown selected, using javascript

Posted on 2008-06-18
5
19,753 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now