SOS

Shane O’Sullivan’s technical blog… really ties the room together

Getting the selected ID from a Dojo ComboBox

Posted by Shane O'Sullivan on 30 July, 2006

While fiddling around with the Dojo ComboBox widget today, I noticed that while it does support name-value pairs, it doesn’t provide an explicit way of getting back the “value”. For example, if you provide it with the JSON object:

[ {"Pat":1}, {"Mary":2}]

you can use the ComboBox’s getValue() function to retrieve the names “Pat” and “Mary”, but not the second value, which is generally what you really want.

So, FYI, to get the value, get a handle to the ComboBox, and retrieve it’s comboBoxSelectionValue.value attribute. For example, given a ComboBox with a widgetId of “myCombo”, use

var selectedId = dojo.widget.byId(’myCombo’).comboBoxSelectionValue.value;

Alternatively, and possibly simpler, you can set the setSelectedValue attribute on the div when you’re creating the ComboBox. For example, if I had a hidden input field that I wanted the selected id to be copied to:

<input type=”hidden” name=”userid” id=”userid”></input>

you would define the ComboBox like:

<div dojoType=”ComboBox”

setSelectedValue=”dojo.byId(’userid’).value=arguments[0]” …….(whatever other args you want)

> </div>

This results in the hidden input field being automatically populated with the selected id value. Simple enough, but it would be better if the ComboBox had a parameter where you could simply give it the id of an input node to mirror itself to.

22 Responses to “Getting the selected ID from a Dojo ComboBox”

  1. Mr Moon Says:

    Thank you very much , 5* rating

  2. Shane O'Sullivan Says:

    you’re very welcome, glad this helped

  3. Ryno Says:

    Awesome work, you’ve saved me potentially hours of frustration!

  4. DavidP Says:

    Sweet. Just what i was looking for. Mucho appreciated.

  5. Shane O'Sullivan Says:

    Glad to help :-)

  6. Bruno Says:

    Thank you very much, it was very helpful.

  7. Daniel Chirita Says:

    Great idea!

    Only one observation: in order for the dojo.widget.byId to work on a Combobox created from existing markup, the markup should be constructed using “” and not “” as it is presented in the manual.

  8. Shane O'Sullivan Says:

    If you’re entering markup into a comment, you’ll probably have to escape the angle brackets, use &lt; for < and &gt; for >

  9. Daniel Chirita Says:

    Yes, you are right.

    What I meant was:
    “<select>” and not
    “<input>”.

  10. Zenaida Says:

    I’m sorry, but I prefer to have a complete sample.

    Can you, please, publish a sample, or send me a sample on this to asd [_at_] 37 [_dot_] com?

    Thank you,

    Zenaida

  11. Shane O'Sullivan Says:

    Um, ok…. my working rate is €100 per hour, but just for you I’ll make it €200. Please send a cheque to
    1 AreYouKiddingMe St.,
    GetOffYourAss,
    AndDoSomeWork,
    Co. Cork,
    Ireland

    This is open source, which means we work for free, giving our free time for the benefit of ourselves and the community. In the open source world, there is little in the way of hand holding, you’re expected to contribute back, and to work hard. I suggest you try reading this post again, and applying it to your problem.

    Then you can figure it out, and publish your own sample for others to learn from. You can even post it here as a comment. Hmm, it took me 4 mnutes to type this, that’ll be 30 bucks please….

  12. Rai Singh Says:

    Very nice. Thank you!

  13. Shane O'Sullivan Says:

    Rai Singh, you’re very welcome

  14. Michael Smith Says:

    It’s nice to see these kinds of answers on blogs. However, isn’t the Dojo Select widget meant to solve this problem? The Select widget provides the getLabel(), setLabel(), setValue(), and getValue() (inherited from ComboBox) functions. I’m surprised you didn’t mention this at all. At any rate, thanks for the article.

  15. Monthy Says:

    Super thanks. Great post just what I’ve been looking for.

  16. Randy May Says:

    Excellent!

    Thanks

  17. elias Says:

    Great Post, thx!

    (I had to add the dataUrl=”xxx.json” to the div… don’t know if this is obvious… ;)

  18. Ashley Says:

    I also really appreciate the info, dojo docs are not so great and this is exactly what I needed to know.

    But this {{”Pat”:1},{”Mary”:2}} is not legal JSON. I think you mean [{"Pat":1},{"Mary":2}] or better for security {”myData”:[{"Pat":1},{"Mary":2}]}.

  19. Shane O'Sullivan Says:

    Hi Ashley,

    Yes, you are correct of course, that was not valid JSON. I’ve updated it.

    Thanks,

    Shane

  20. Tattybogle Says:

    Do you know if there’s a way to retrieve Pat | Mary by using the value?

    I have a value being passed in via the url (e.g. 1) and want to initialise the combobox to the appropriate value (i.e. ‘Pat’ ;)

    Thanks

  21. Tattybogle Says:

    P.S. I’m still on 0.4 (don’t ask… ;)

  22. Shane O'Sullivan Says:

    There is a way, but it depends on the data store you are using. If you are using the dojo.widget.basicComboBoxDataProvider, it has an variable called “_data” that contains an array mapping display value to hidden value.

    You can search it using something like:

    var searchVal = “some string”;
    var array = myCombo.dataProvider._data;
    for(var i = 0; i < array.length; i++) {
    if(array[i][1] == searchVal) {
    myCombo.setAllValues(array[i][0], array[i][1]);
    }
    }

    Hope this helps

    Shane

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>