dojo icon

Dojo - Setting the ComboBox Default Value

Posted in , , Wed, 14 Jun 2006 08:39:00 GMT

Dojo Toolkit's ComboBox doesn't take a default value but one can be written in using JavaScript. The default value in the ComboBox dojoType is lost during onLoad when the dojoType widget is rendered. To get around this we can use JavaScript to fill in the value during dojo.addOnLoad. Here's the relevant JavaScript:

function init() {
  var mycombobox = dojo.widget.byId('mycombobox');
  mycombobox.textInputNode.value = 'Seattle';
}

// method 1
dojo.addOnLoad( init );

// method 2
dojo.addOnLoad( function() {
  init()
} )
// won't work b/c you want to send an object
// not call a function
dojo.addOnLoad( init() );

The HTML:

<input dojoType="ComboBox"
  id="mycombobox"
  value="this should never be seen - it is replaced!"
  dataUrl="comboBoxData.js" style="width: 300px;" name="foo.bar"
  onValueChanged="setVal2"
>

A more detailed example of this technique is available on the wiki at Dojo Toolkit: Setting the Combobox Default Value.

del.icio.us:Dojo - Setting the ComboBox Default Value digg:Dojo - Setting the ComboBox Default Value reddit:Dojo - Setting the ComboBox Default Value spurl:Dojo - Setting the ComboBox Default Value wists:Dojo - Setting the ComboBox Default Value simpy:Dojo - Setting the ComboBox Default Value newsvine:Dojo - Setting the ComboBox Default Value blinklist:Dojo - Setting the ComboBox Default Value furl:Dojo - Setting the ComboBox Default Value fark:Dojo - Setting the ComboBox Default Value blogmarks:Dojo - Setting the ComboBox Default Value Y!:Dojo - Setting the ComboBox Default Value smarking:Dojo - Setting the ComboBox Default Value magnolia:Dojo - Setting the ComboBox Default Value segnalo:Dojo - Setting the ComboBox Default Value

3 comments

Comments

  1. Ian said 3 months later:

    I believe the best way to do it would be

    dojo.widget.byId(‘mycombobox’).setValue(‘Seattle’)

    Since directly overwriting the textInputNode.value does not notify the ComboBox of the change, and thus doesn’t update it’s internals; this means that if the user does not interact with the ComboBox (which itself calls setValue()) then it will not submit the form properly.

    Unfortunately, I was unable to create an account on the Wiki to update that.

  2. valmy said 3 months later:

    If text in the combo box options are different than the actual values, I find it necessary to use:

    dojo.widget.byId(‘mycombobox’).setValue(‘Seattle’); dojo.widget.byId(‘mycombobox’).setSelectedValue(5);

    (assuming the actual value for Seattle is 5).

  3. ajit said about 1 year later:

    how to set the value to filtering select combobox from data base….......

(leave url/email »)

   Comment Markup Help Preview comment