My code works in IE but breaks in Safari, Firefox, and Opera. (big surprise)
document.getElementById("DropList").options.length=0;
After searching, I've learned that it's the length=0
that it doesn't like.
I've tried ...options=null
and var clear=0; ...length=clear
with the same result.
I am doing this to multiple objects at a time, so I am looking for some lightweight JS code.
The items should be removed in reverse, otherwise it will cause an error. Also, I do not recommended simply setting the values to
null
, as that may cause unexpected behaviour.Or if you prefer, you can make it a function:
You can use the following to clear all the elements. Note that
This is the shortest way:
One line, no for, no JQuery, simple.
To remove the options of a select html object, you can use this piece of code:
This will work in all browsers. =)