Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the value (selected option) of a dijit.form.Select widget

I have a dijit.form.Select widget. It's tied to a data store, if that matters. It's filled with several options already. All I want to do is programmatically set its value. I can get its value using myWidget.attr('value') but if I try to do myWidget.attr('value', 5) for example (where 5 is one of the valid values), all it does is reset the widget to select the very first option, no matter what value I give it.

This seems to be a bug, and there aren't any tests or documentation which show how to accomplish what I want to. But is there some way, even if it's a dirty hack?

I'm using Dojo 1.4.0. Note that dijit.form.Select is the new name for dojox.form.DropDownSelect.

edit: I even tried resetting the widget with all new options, but it ignores the option which has selected = true and just selects the first option. There must still be a way though.

like image 665
aehlke Avatar asked Feb 07 '10 04:02

aehlke


3 Answers

Even if your values are ints, if you set your integer to a string then this will work.

dijit.byId( 'my_select' ).attr( 'value', String( 5 ) );
like image 123
voidstate Avatar answered Nov 01 '22 20:11

voidstate


Turns out it's a bug - if the option values aren't strings, it won't work (mine were integers).

like image 11
aehlke Avatar answered Nov 01 '22 21:11

aehlke


Repost of my comment: There is a test page here: dojo archive that you can mess with. Using fire-bug I used dijit.byId('s9').attr('value', 'CO') successfully on that page. That will set the "store-based" Select on that page.

But as you said I set it using a string and you were using integers so I didn't see the bug. Good catch.

like image 2
jbarz Avatar answered Nov 01 '22 19:11

jbarz