JQuery to Set Dropdownlist list Item (value / text) (ASP.NET)

a sample dropdownlist

<asp:dropdownlist id="ddl" runat="server">
<asp:listitem text="a" value="0" />
<asp:listitem text="c" value="1" />
<asp:listitem text="z" value="2" />
</asp:dropdownlist>

now if you want to choose an item based on an event let us have button click as an example

<asp:button id="btnclick" runat="server" Text="click" />

now in the jquery part you have to do like this

   1:  <script type="text/javascript">
   2:   $('#<%=btnclick.ClientId%>').click(function(){
   3:   
   4:  $("#<%=ddl.ClientId %> option[text='c' ] ").attr("selected","selected");
   5:  //for selecting based on value
   6:  $("#<%=ddl.ClientId %> option[value='1' ] ").attr("selected","selected"); 
   7:  //or
   8:  $("#<%=ddl.ClientId %>").val('0').attr("selected","selected"); 
Technorati Tags: ,,
   9:   
  10:  });
  11:  </script>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.