how to remove all options from a dijit/form/select?

1.9k views Asked by At

I use a very simple dijit/form/select on my webppage I add few options by code like this :

option1 = { value: "o1", label: "option 1", selected: false };
option2 = { value: "o2", label: "option 2", selected: true };

this.mySelect.addOption([option1, option2]);   

It's working. However when I try to clear my dijit/form/select using this code :

this.mySelect.removeOption(this.mySelect.getOption());

All my options are gone except the selected one.

I try using a .reset or even a .value = '' but nothing worked.

So how to remove all options from a dijit/form/select ?

1

There are 1 answers

1
Hackerman On BEST ANSWER

Actually the answer is pretty simple, but not well docummented (it's pretty hard to find, like all dojo related stuff)

this.mySelect.removeOption(lang.clone(option1));
this.mySelect.removeOption(lang.clone(option2));

this.mySelect.store = null;
this.mySelect.set('value', '');
this.mySelect._setDisplay(""); //This line alone should do the trick

As in the comments, the last line should do the trick, but this way you can be sure that the select element gets cleared.

Demo: JSFiddle demo