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.
For Vanilla JavaScript there is simple and elegant way to do this:
If you wish to have a lightweight script, then go for jQuery. In jQuery, the solution for removing all options will be like:
This can be used to clear options:
If you are using JQuery and your select control has ID "DropList" you can remove its options doing this way:
Actually it works for me with any option list, like datalist.
Hope it helps.
Note that a select can have both
- optgroup &
- options collection
as its children.
So,
Method #1
Method #2
I tested, both work on Chrome.
I like the simpler, the old fashioned, method #1.