posted 7/6/2011 by Raghav Khunger
Today a person asked aquestion on the forums on how to select the first item of dropdownlist with jQuery. I decided to write a quick blog on the code which I used to set it.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript"> $(function() { $('select[id$=DropDownList1] option:first-child').attr("selected", "selected"); }); </script> </head> <body> <form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="Text1" Value="1" runat="server"></asp:ListItem> <asp:ListItem Text="Text2" Value="2" runat="server"></asp:ListItem> <asp:ListItem Text="Text3" Value="3" runat="server"></asp:ListItem> <asp:ListItem Text="Text4" Value="4" runat="server"></asp:ListItem> </asp:DropDownList> </form> </body> </html>
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18