Skip to content
Amal Hashim
All posts
Snippet

jQuery Clearing DropDown

The fastest way to empty a select element: replace it with a shallow copy of itself.

1 min read#JavaScript#jQuery

Removing options one by one from a large select is slow. A faster way is to clone the select without its children and swap it in.

js
function ClearOptions(id) {
    var selectObj = document.getElementById(id);
    var selectParentNode = selectObj.parentNode;
    var newSelectObj = selectObj.cloneNode(false); // shallow copy, no options
    selectParentNode.replaceChild(newSelectObj, selectObj);
    return newSelectObj;
}

Usage:

js
ClearOptions('ddlSelectTagId');

The function returns the new select element. Any event handlers attached to the old element are not copied, so re-attach them if you need them.