Friday, October 19, 2012 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 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="" >
<head runat="server">
    <script type="text/javascript"
    <script type="text/javascript"
    $(document).ready(function() {
      $('#drpCountry option[value=USA]').text('America');
    <form id="fromDropDownList" runat="server">
        <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"
Text="Change Items" />

Search Flipkart Products:

No comments: