How to clear HTML data list current options?
10,129
Solution 1
In this instance, you don't want to remove schoolNameList
itself; you want to remove the children of that list (the list items). There are a few ways to do this, but this one should work:
document.getElementById('schoolNameList').innerHTML = '';
Solution 2
I like this one. Seems the cleanest I could find, but it is jQuery not vanilla JS DOM.
$('#schoolNameList').empty();
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on June 05, 2022Comments
-
Admin about 2 years
I am writing an dynamic data list. However, when I tried to update the list, the previous didn't clear. Are there any solutions?
Here is my code
function loadDataList(selectedSchoolName) { var options = ''; //document.getElementById('schoolNameList').remove(); for(var i = 0; i < selectedSchoolName.length; i++) { options += '<option value="'+ selectedSchoolName[i] +'" >'; } document.getElementById('schoolNameList').innerHTML = options; }
Thank You