Remove an item from DropDownList
12 Apr 20225 minutes to read
@Html.EJS().Button("first").Content("Remove 0th Item").Render()
@Html.EJS().DropDownList("games").Placeholder("Select a game").PopupHeight("200px").DataSource((IEnumerable<object>)ViewBag.data).Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "Game" }).Render()
<script>
document.getElementById('first').onclick = () => {
// create DropDownList object
let obj = document.getElementById('games');
if (obj.ej2_instances[0].list) {
// Remove the selected value if 0th index selected
var dropObject = document.getElementById("games");
var dropDownListObject = dropObject.ej2_instances[0];
if (dropDownListObject.index === 0) {
dropDownListObject.value = null;
dropDownListObject.dataBind();
}
// remove first item in list
(obj.ej2_instances[0].list.querySelectorAll('li')[0]).remove();
if (!obj.ej2_instances[0].list.querySelectorAll('li')[0]) {
dropDownListObject.dataSource = [];
// enable the nodata template when no data source is empty.
obj.ej2_instances[0].list.classList.add('e-nodata');
}
}
else {
// remove first item in list
dropDownListObject.dataSource.splice(0, 1);
}
};
</script>