Remove Item from Drop Down List using JS


<script type="text/javascript">
function removeListItem()
{
      var htmlSelect=document.getElementById('selectYear');
      if(htmlSelect.options.length==0)
     {
           alert('You have removed all options');
            return false;
      }
      var optionToRemove=htmlSelect.options.selectedIndex;
      htmlSelect.remove(optionToRemove);
      alert('The selected option haas been removed successfully');
      return true;
}


<asp:DropDownList ID="selectYear" runat="server">
<asp:ListItem Value="2000">2000</asp:ListItem>
<asp:ListItem Value="2001">2001</asp:ListItem>
<asp:ListItem Value="2002">2002</asp:ListItem>
<asp:ListItem Value="2003">2003</asp:ListItem>
<asp:ListItem Value="2004">2004</asp:ListItem>

</asp:DropDownList>

<input name="btnRemoveItem" type="button" id="btnRemoveItem" value="Remove Option" onClick="javascript:removeListItem();" />


Comments