14

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.

aehlke
  • 15,225
  • 5
  • 36
  • 45
  • There is a test page here: http://bit.ly/9qitSk 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. – jbarz Feb 15 '10 at 23:12
  • @Joe B Awesome thanks - I'd seen that before, but didn't see a value setting example. I had tried doing that in firebug with my own Select widget, which didn't work, so I wonder what's different... – aehlke Feb 18 '10 at 09:15
  • BTW post that as an answer and I'll upvote you. – aehlke Feb 18 '10 at 09:16

3 Answers3

15

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 ) );
voidstate
  • 7,937
  • 4
  • 40
  • 52
11

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

aehlke
  • 15,225
  • 5
  • 36
  • 45
  • 3
    It's actually a side effect of how arrays/hashes work in js, I think. if you use an int, it will think you're indexing the array, instead of using it as a hash. – aehlke Apr 10 '10 at 18:20
2

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.

ivandov
  • 619
  • 8
  • 14
jbarz
  • 582
  • 6
  • 17
  • I'm seeing the exact same problem (but trying to click on an item in the dropdown). Here I'm using Dojo 1.7.3 and most definitely using Stringrs! :/ – James Mills Oct 30 '12 at 22:35