开发者

Javascript clear

Is there a Javascript Clear function for a dropdownlist per se?

ddlist.Clear(); or something 开发者_Python百科of that sort?


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.

http://docs.jquery.com/Manipulation/empty


If by "clear the values" you mean remove all the dropdown <option> elements, the quickest and most concise way is:

ddlist.options.length = 0


no, there isn't. but you could do so like this:

function clearDropDownList(ddl) {
    while (ddl.hasChildNodes()) {
        ddl.removeChild(ddl.lastChild);
    }
}


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);
}


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


Short.

while (ddlist.options.length) 
     ddlist.options.remove(0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜