Javascript onchange Event to get SelectedIndex SelectedValue SelectedText of DropdownList C# VB.Net

asp:DropDownList is the server side control of commonly known as Combo Box. Most of the times developers need to find or get the Selected Index value, Selected Value & Selected Text. In my previous post i have described “Jquery to get SelectedValue SelectedIndex SelectedText of a DropdownList C# VB.Net“. In this post I will describe to read DropdownList SelectedIndex, SelectedValue & SelectedText using Javascript. Hope it will help & save time for developers.

Desired Output:
Javascript_ Selectedindex_Selectedvalue_Selectedtext_Dropdownlist_Csharp_VB.Net_chrome

To Complete the Javascript Code Example First Add asp:DropDownList Markup:

<h2>To display Selectedvalue, Selectedindex & Selectedtext please select an item from the DropDownList</h2>
<br />
<br />
<br />

<asp:DropDownList id="aspdropdown" runat="server" onchange="getDropdownListSelectedText();">
    <asp:ListItem Value="Asp" Text="Free articles/Code examples"></asp:ListItem>
    <asp:ListItem Value="Sqlserver" Text="Free Sql server articles/Code examples"></asp:ListItem>
    <asp:ListItem Value="Javascript" Text="Free Javascript articles/Code examples"></asp:ListItem>
    <asp:ListItem Value="XML" Text="Free XML articles/Code examples"></asp:ListItem>
    <asp:ListItem Value="Gridview" Text="Free Gridview articles/Code examples"></asp:ListItem>
<hr />
<br />
<br />
<asp:Label runat="server" ID="lblDropdownList" Font-Bold="true"></asp:Label>

Now write the below Javascript Function:

<script type="text/javascript">
    function getDropdownListSelectedText() {
        var DropdownList = document.getElementById('<%=aspdropdown.ClientID %>');
        var SelectedIndex = DropdownList.selectedIndex;
        var SelectedValue = DropdownList.value;
        var SelectedText = DropdownList.options[DropdownList.selectedIndex].text;

        var LabelDropdownList = document.getElementById('<%=lblDropdownList.ClientID %>');
        var sValue = 'Selected Index: ' + SelectedIndex + '<br/> Selected Value: ' + SelectedValue + '<br/> Selected Text: ' + SelectedText;

        LabelDropdownList.innerHTML = sValue;

Note: No server side code is required to test the example.

If you look at the javascript client side code you will found three different variables i have used to collect Selected Index, Selected Value & Selected Text value. You can use one of them which is required for you. Now you can do anything based on user selection such as writing the selected text into textbox, populating another dropdownlist, Using as a parameter in sql server query for filtering purposes etc…

Script tested for below browsers:
1. Internet Explorer
2. Mozilla Firefox
3. Opera
4. Google Chrome

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, Client, Javascript, VB.Net
4 comments on “Javascript onchange Event to get SelectedIndex SelectedValue SelectedText of DropdownList C# VB.Net
  1. Marco Antonio says:

    great, this article helped me a lot in my work

  2. Sahan says:

    Thanks a million.It worked for me

  3. peter says:

    How to make a javasript worked if the asp dropdown and textbox property is runat server.

  4. Ahmet says:

    thank you very much

Leave a Reply

Your email address will not be published. Required fields are marked *