Set DropDownList value based on text/value in jQuery

Category > JQUERY || Published on : Friday, December 4, 2015 || Views: 10168 || Set DropDownList value based on text/value in jQuery


Introduction

Here Pawan Kumar will explain how to In this article i will explain how to set DropDownList value based on text/value in jQuery.

Description

In previous post I have explained Zoom Feature for Query Text and Results Text in SQL Server Management Studio 2012, How to make How to make blinking/flashing text with CSS3 and jQuery, How to replace number from a string in C#, How to create draw a doughnut chart using ChartJS in Asp.net detailed Example, Code Snippet for Deleting a SQL Server stored procedure, Bootstrap style DropDownList example in ASP.Net, and many more articles.

Now I will explain How to Set DropDownList value based on text/value in jQuery

So follow the steps to learn Set DropDownList value based on text/value in jQuery

Complete Source Code:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>How to get DropDownList value based on text/value in jQuery</title>
    <script src="Scripts/jquery-1.11.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function SetDropDownByText() {
            // Now set dropdown selected option to 'Admin'.
            var selectedText = 'Admin';
            $('#ddlRole option').map(function () {
                if ($(this).text() == selectedText) return this;
            }).attr('selected', 'selected');
        }
        function SetDropDownByValue() {
            var selectedValue = '3';
            $('#ddlRole option').map(function () {
                if ($(this).val() == selectedValue) return this;
            }).attr('selected', 'selected');
        }
    </script>
</head>
<body>
    <div>
        <select id="ddlRole">
            <option value="-1">Select</option>
            <option value="1">User</option>
            <option value="2">Admin</option>
            <option value="3">Super Admin</option>
        </select>
        <input type="button" value="Set By Text" onclick="SetDropDownByText()" />
        <input type="button" value="Set By Value" onclick="SetDropDownByValue()" />
    </div>
</body>
</html>

 

Conclusion:

So, In this tutorial we have learned, Set DropDownList value based on text/value in jQuery