Friday, October 19, 2012

Asp.net jQuery dropdown change Text value


In this post, we will see on how to change the Text values of the items in a DropDown

To change the text values of the dropdown list items we can use the text() function as follows.  

$('yourDropDown option[value=1]').text ('New Text');

Here is a full example


Create a new Asp.net page, copy the entire code below, change reference of the jQuery file src="JavaScript/jquery-1.7.2.js" to point to your local directory. Build and run the application, click on the Change Items buttons and notice that the display text for the option USA gets changed from United States to America in the drpCountry dropdown.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>DropDownList</title>
    <script type="text/javascript"
src="JavaScript/jquery-1.7.2.js"></script>
    <script type="text/javascript"
language="javascript">
    $(document).ready(function() {
        $('#cmdChangeItems).click(function(event)
        {
            event.preventDefault();
      $('#drpCountry option[value=USA]').text('America');
      });
    </script>
</head>
<body>
    <form id="fromDropDownList" runat="server">
    <div>
        Country:
        <asp:DropDownList ID="drpCountry" runat="server">
            <asp:ListItem Value="USA">United States</asp:ListItem>
            <asp:ListItem Value="UK">United Kingdom</asp:ListItem>
        </asp:DropDownList><br /><br />       
        <asp:Button ID="cmdChangeItems"
runat="server"
Text="Change Items" />
    </div>
    </form>
</body>
</html>


Search Flipkart Products:
Flipkart.com

No comments: