Skip to content

Commit cecb098

Browse files
authored
Initial
This method clones and replace childs. The removed select element is simply discarded and the memory is free by the browser when doing garbage collection, a new one is shallow cloned from the old one and inserted back into the page. This method is an effective option for very large, medium or small select boxes.
1 parent 8518ff0 commit cecb098

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

clearSelectOptions.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This function clear all the options in an HTML select statement
2+
// Provide the 'id' of the select element
3+
// References to old select will become invalidated
4+
// Returns a reference to the new select object
5+
6+
function clearSelectOptionsFast(id) {
7+
var select = document.getElementById('idtoClean')
8+
var selectParentNode = select.parentNode
9+
var newSelectObj = select.cloneNode(false)
10+
selectParentNode.replaceChild(newSelectObj, select)
11+
//Use this line if you don't want use a return reference function
12+
//select = newSelectObj
13+
return newSelectObj
14+
}
15+
16+
// This is an alternative, simpler method.
17+
// It does appear to be a fast method
18+
// Tested in Firefox
19+
function clearSelectOptionsFastAlt(id) {
20+
document.getElementById(id).innerHTML = "";
21+
}

0 commit comments

Comments
 (0)