user1268740
user1268740

Reputation: 113

How to add items to a combobox in dojo?

I've a form with two combo boxes. one is normal and the other is dijit.form.Combobox.

<select id="mySel"></select>

<select dojotype="dijit.form.ComboBox" id="dsel"/>

I'm able to add items to the normal one using the following script.

var opt = mySel.appendChild(document.createElement('option'));
opt.text = "My sample text"

But the same is not working in the dijit combo. How to do this ?

Upvotes: 3

Views: 3015

Answers (1)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44665

The problem is that Dojo parses the DOM node (the <select>) to something completely different. Dojo uses a store internally to save the options of your combobox.

To add options to the combobox you need to access the store of the combobox and add new items to it. You can read more about it at the Dojo API of the ComboBox or the Store API.

At the store API you will notice a function called add(). If you use that function, you can add new items to the store. The code would be something like:

registry.byId("dsel").get('store').add({ name: "Test", id: 1 });

or a complete JSFiddle can be found here

Upvotes: 4

Related Questions