I have the following code on an ASP.net page:
<asp:DropDownList ClientIDMode="Static" ID="ddl1" CssClass="chosen-select le" runat="server" AppendDataBoundItems="true"></asp:DropDownList> <asp:DropDownList ClientIDMode="Static" ID="ddl2" CssClass="chosen-select le" runat="server" AppendDataBoundItems="true"></asp:DropDownList> <asp:DropDownList ClientIDMode="Static" ID="ddl3" CssClass="chosen-select le" runat="server" AppendDataBoundItems="true"></asp:DropDownList> <input type="button" id="ClearForm" value="Clear" class="btn1" />
JQuery to set the index to 0 for all three drop-down lists:
$(function () { $("body").on("click", "#ClearForm", function (e) { e.preventDefault(); $("select#ddl1, select#ddl2, select#ddl3").prop('selectedIndex', 0);
How do I change so that the dropdownlist index is set to 0.
HTML rendering of the first drop-down list:
<select name="ctl00$BodyPlaceHolder$ddl1" id="ddl1" class="le"> <option value="Select a State">Select a State</option> <option value="Alabama">AL</option> <option value="Alaska">AK</option> <option value="Arizona">AZ</option> </select>
I tried the following jQuery and it returns null :
alert($("select#ddl1").val());
Also tried the following and it did not work:
$('select').each(function () { $(this).find('option:first').prop('selected', 'selected'); });
The following JavaScript has been added:
function setSelectedIndex(dropdownlist, selVal) { var ddl1 = document.getElementById(dropdownlist); alert(ddl1.selectedIndex); if (ddl1.length >= selVal) {
It looks like if I delete the selected jQuery, it works fine: http://jsfiddle.net/bpbvhsay/
SearchForKnowledge Apr 15 '15 at 14:45 2015-04-15 14:45
source share