empty a select input using js

Solutions on MaxInterview for empty a select input using js by the best coders in the world

showing results for - "empty a select input using js"
Elisa
11 Mar 2017
1// Fast javascript function to clear all the options in an HTML select element
2// Provide the id of the select element
3// References to the old <select> object will become invalidated!
4// This function returns a reference to the new select object.
5function ClearOptionsFast(id)
6{
7	var selectObj = document.getElementById(id);
8	var selectParentNode = selectObj.parentNode;
9	var newSelectObj = selectObj.cloneNode(false); // Make a shallow copy
10	selectParentNode.replaceChild(newSelectObj, selectObj);
11	return newSelectObj;
12}
13
14// This is an alternative, simpler method.  Thanks to Victor T.
15// It does not appear to be as fast as the ClearOptionsFast method in FF 3.6.
16function ClearOptionsFastAlt(id)
17{
18	document.getElementById(id).innerHTML = "";
19}
20
21