Mick White wrote:
trope wrote: Does anyone have the code to clear all of the elements in a select
menu?
I've been having trouble with this following code:
for (var i=0; i< 100; i++)
document.addskill.cat2.options[i] = null;
because it doesn't clear the elements.
document.addskill.cat2.options.length=0
Does not work in all cases, therefore my dhtml.js, version 0.9.2005050817,
contains
/**
* Removes all options from a HTMLSelectElement object.
*
* @author
* (C) 2003 Thomas Lahn <dh******@PointedEars.de>
* @partof
*
http://pointedears.de/scripts/dhtml.js
* @param oSelect : HTMLSelectElement
* Reference to a HTMLSelectElement object.
* @param bAllowReload : boolean
* If <code>true</code>, reloads the document.
* @return type boolean
* <code>true</code> if successful, <code>false</code>
* otherwise.
*/
function removeOptions(oSelect, bAllowReload)
{
if (oSelect
&& oSelect.tagName
&& oSelect.tagName.toLowerCase() == "select")
{
var o = oSelect.options;
if (o && o.length)
{
// shortcut if "length" property is not read-only
o.length = 0;
while (o.length > 0)
{
if (o.remove)
{
o.remove(o.length - 1);
}
else
{
o[o.length - 1] = null;
if (bAllowReload)
{
history.go(0);
}
}
}
return true;
}
}
return false;
}
Note that the remove() method suggested in
<11*********************@g49g2000cwa.googlegroups. com>
presents a proprietary approach (IE only AFAIK) which
therefore must be feature-tested before.
PointedEars