Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get the value and text of dropdown selected, using javascript

Posted on 2008-06-18
5
Medium Priority
?
19,815 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 750 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

704 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