Saturday 22 March 2014

get dropdown value in jquery

The .val() method is primarily used to get the values of form elements such as input, select and textarea, And to get the text of the dropdown values by jquery need to use .text() method.
@Code snippet how to get Value of dropdown in Jquery
<head runat="server">
    <title>Validation by Javascript</title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#ddlvalues").change(function () {
                alert($("#ddlvalues option:selected").val()); //get Value of dropdown in Jquery
                alert($("#ddlvalues option:selected").text()); //get text of dropdown in Jquery
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="60%" align="center">
            <tr>
                <td colspan="2" align="center">
                    <h1>
                        Validation by Javascript</h1>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Get Value
                </td>
                <td align="left">
                    <asp:DropDownList ID="ddlvalues" ClientIDMode="Static" runat="server">
                        <asp:ListItem Text="first" Value="1"></asp:ListItem>
                        <asp:ListItem Text="second" Value="2"></asp:ListItem>
                        <asp:ListItem Text="Third" Value="3"></asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
           
        </table>
    </div>
    </form>
</body>
@Screen out put in Browser

No comments:

Post a Comment