Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get the value and text of dropdown selected, using javascript

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

971 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