Home > Uncategorized > JQuery to get ASP.Net DropDownList Selected item Value, Text and Index

JQuery to get ASP.Net DropDownList Selected item Value, Text and Index


Hi all,

Try this example to use JQuery to get ASP.Net DropDownList Selected item Value, Text and Index

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Example to get DropDownList selected item index, value and text</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%=ddlWeekDays.ClientID %>').on("change", function () {
                //Get DropDownlist selected item index
                var selectedIndex = $(this).find("option:selected").index();
                //Get DropDownlist selected item value
                var selectedValue = $(this).find("option:selected").val();
                //Get DropDownlist selected item text
                var selectedText = $(this).find("option:selected").text();
                $("#spnIndex").text(selectedIndex);
                $("#spnValue").text(selectedValue);
                $("#spnText").text(selectedText);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width: 380px; height: 150px;">
                <legend>Get DropDownList Selected item Index, Value and Text</legend>
                <table>
                    <tr>
                        <td>Select Day:
                    <asp:DropDownList ID="ddlWeekDays" runat="server">
                        <asp:ListItem Value="0" Text="Select"></asp:ListItem>
                        <asp:ListItem Value="1" Text="Monday"></asp:ListItem>
                        <asp:ListItem Value="2" Text="Tuesday"></asp:ListItem>
                        <asp:ListItem Value="3" Text="Wednesday"></asp:ListItem>
                        <asp:ListItem Value="4" Text="Thursday"></asp:ListItem>
                        <asp:ListItem Value="5" Text="Friday"></asp:ListItem>
                        <asp:ListItem Value="6" Text="Saturday"></asp:ListItem>
                        <asp:ListItem Value="7" Text="Sunday"></asp:ListItem>
                    </asp:DropDownList>
                        </td>
                        <td>Selected Index :
                        </td>
                        <td>
                            <span id="spnIndex"></span>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>Selected Value :
                        </td>
                        <td>
                            <span id="spnValue"></span>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>Selected Text :
                        </td>
                        <td>
                            <span id="spnText"></span>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>
    </form>
</body>
</html>

Hope this helps

Good Luck.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: