Hello all,
I am developing a simple prototype which is inspired by this article jQuery API Documentation
I have the following code in the aspx markup page
<asp:DropDownList ID="DropDownList_1" runat="server">
<asp:ListItem Text="[ == Please Select == ]" Value="" />
<asp:ListItem Text="TOYOTA" Value="TOYOTA" />
<asp:ListItem Text="MERCEDES-BENZ" Value="MERCEDES-BENZ" />
<asp:ListItem Text="BMW" Value="BMW" />
<asp:ListItem Text="HONDA" Value="HONDA" />
<asp:ListItem Text="FORD" Value="FORD" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList_2" runat="server">
<asp:ListItem Text="[ == Please Select == ]" Value="" />
<asp:ListItem Text="Available" Value="Available" />
<asp:ListItem Text="Not available" Value="Not available" />
</asp:DropDownList>
I would like to remove certain values from the DropDownList DropDownList_2 cloned if certain conditions are true.
if(DropDownList_1.value=="HONDA"){
// remove Available value from the DropDownList DropDownList_2 cloned
}
Output
//DROP DOWN LIST CLONED FROM DropDownList_2
<asp:DropDownList ID="DropDownList_2_x" runat="server">
<asp:ListItem Text="[ == Please Select == ]" Value="" />
<asp:ListItem Text="Not available" Value="Not available" />
</asp:DropDownList>
How can I do this using jquery
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=btnClone]").bind("click", function () {
var index = $("#container select").length + 1;
//Clone the DropDownList
var DropDownList_2 = $("select[id$=DropDownList_2]").clone();
DropDownList_2.attr("id", "DropDownList_2_" + index);
DropDownList_2.attr("name", "DropDownList_2_" + index);
var selectedValue = $("select[id$=DropDownList_2] option:selected").val();
DropDownList_2.find("option[value = '" + selectedValue + "']").attr("selected", "selected");
//Append to the DIV.
$("#container").append(DropDownList_2);
$("#container").append("<br /><br />");
return false;
});
});
</script>
<div id="container">
<asp:Button ID="btnClone" Text="Clone" runat="server" />
</div>