PROGRAMING/JAVASCRIPT

[JAVASCRIPT] SelectBox 값 삭제...

donghunl 2011. 7. 15. 16:29
반응형

// Standard javascript function to clear all the options in an HTML select element
// In this method, you provide the id of the select dropdown box
function ClearOptions(id)
{
 document.getElementById(id).options.length = 0;
}
 
// Standard javascript function to clear all the options in an HTML select element
// In this method, you just provide the form name and dropdown box name
function ClearOptionsAlt(FormName, SelectName)
{
 document.forms[FormName].elements[SelectName].options.length = 0;
}
 
// Fast javascript function to clear all the options in an HTML select element
// Provide the id of the select element
// References to the old <select> object will become invalidated!
// This function returns a reference to the new select object.
function ClearOptionsFast(id)
{
 var selectObj = document.getElementById(id);
 var selectParentNode = selectObj.parentNode;
 var newSelectObj = selectObj.cloneNode(false); // Make a shallow copy
 selectParentNode.replaceChild(newSelectObj, selectObj);
 return newSelectObj;
}
 
// This is an alternative, simpler method.  Thanks to Victor T.
// It does not appear to be as fast as the ClearOptionsFast method in FF 3.6.
function ClearOptionsFastAlt(id)
{
 document.getElementById(id).innerHTML = "";
}
Dropdown List
Test Buttons    
       
Timing Results
반응형