Search This Blog

Friday 5 February 2016

Drop down selected text in javascript

var dropDownList = document.getElementById("DropDownList1");

var selectedText = dropDownList.options[dropDownList.selectedIndex].text;

Step 1:- JavaScript function

        function GetSelectedValueText() {
            var ddl = document.getElementById("DropDownList1");

            var Value = ddl.value;

            var Text = ddl.options[ddl.selectedIndex].text;

            alert("Value " + Value + "\nText " + Text);
        }

Step 2:- Complete Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>drop down selected text in javascript</title>
    <script type="text/javascript">
        function GetSelectedValueText() {
            var ddl = document.getElementById("DropDownList1");

            var Value = ddl.value;

            var Text = ddl.options[ddl.selectedIndex].text;

            alert("Value " + Value + "\nText " + Text);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" ClientIDMode="Static">
                <asp:ListItem Selected="True" Value="0">--Select--</asp:ListItem>
                <asp:ListItem Value="1">India</asp:ListItem>
                <asp:ListItem Value="2">USA</asp:ListItem>
                <asp:ListItem Value="3">UK</asp:ListItem>
                <asp:ListItem Value="4">London</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Get Text Value" OnClientClick="GetSelectedValueText()" />
        </div>
    </form>
</body>
</html>

Step 3:- Output




Note:

Select text from dropdownlist and click on button


No comments:

Post a Comment