Reputation: 125
I have a Dojo Form Select input box where I fill the options using a javascript data object having lable value pairs (example code to build such object below):
for loop {
varStateValuePairs.push({
label: <State ID>,
value: <State Name>
});
}
dijit.byId("StateDDL").addOption(varStateValuePairs);
Now, I want to programmatically select a specific State within this Dojo Form Select. I have tried the following:
dijit.byId("StateDDL").attr("value", String(5)); // 5 is the example value corresponding to the label-value pair I want to select
dijit.byId("StateDDL").attr("value", 5);
dojo.byId("StateDDL").value = 5;
dijit.byId("StateDDL").set("displayedValue", "Texas");
None of the above works. Where am I wrong? I have searched quite a lot and none of the solutions listed in other posts are working for me. I am running Dojo 1.8.
Upvotes: 0
Views: 6554
Reputation: 15104
Use Select.setValue()
.
http://jsfiddle.net/fiddlegrimbo/qauHX/2/
value0
would be selected by default, we select value2
manually.
var varStateValuePairs = [];
for (var i = 0; i < 10; i++) {
varStateValuePairs.push({
label: "state"+i,
value: "value"+i
});
}
require(["dojo/parser", "dijit/registry", "dijit/form/Select", "dojo/domReady!"], function (parser, registry) {
parser.parse().then(function () {
var widget = registry.byId("StateDDL");
widget.addOption(varStateValuePairs);
widget.setValue("value2");
});
});
Upvotes: 2