Is there a Javascript Clear function for a dropdownlist per say.
ddlist.Clear(); or something of that sort?
Is there a Javascript Clear function for a dropdownlist per say.
ddlist.Clear(); or something of that sort?
For an array, you can just use
arr = [];
for elements, etc, you'll need to use a framework or delete each element in a loop
no, there isn't. but you could do so like this:
function clearDropDownList(ddl) {
while (ddl.hasChildNodes()) {
ddl.removeChild(ddl.lastChild);
}
}
I'd recommend checking out jQuery, it has some functionality similar to what you're looking for and can help javascript development in general be easier and behave closer to the same on multiple browsers.
You can set the innerHTML to "", or remove all the option childs programmatically:
var element = document.getElementById("selectId");
element.innerHTML = "";
Or:
var element = document.getElementById("selectId");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
If by "clear the values" you mean remove all the dropdown <option> elements, the quickest and most concise way is:
ddlist.options.length = 0