JavaScript CookBook/Workflow Form/Set the value of a form field via Javascript

From GeoMedia Smart Client
Jump to: navigation, search
GMSC 2013/2014 Javascript API Overview Form Events Types Utility Functions client-side Validation Conditions Interaction with SmartClient Cookbook Cheatsheet


Set the value of a form field via Javascript

Problem: You want to set the value of a form field programmatically after the form has been loaded in the browser.

Solution: Use the .setValue() method of the form field to set its value programmatically. Like with all form-related methods, you have to listen for the form:ready event before you can use it.

Some points to take care of:

  • For List-of-Value based widgets like the Combobox, .setValue() works a bit different: it selects one of the existing choices of the widget. If you want to replace the existing choices with new ones for these widgets, you have to use the .fill() method.
  • The Checkbox widget takes the values 0 (unchecked state) and 1 (checked state).

The following example listens to input changes in TEXTFIELD and writes the reversed input to the textfield REVERT_INPUT. Thus, if the user enters "abc" in TEXTFIELD, the REVERT_INPUT field would contain "cba":

IG.vent.on('form:ready', function(form){
   form.getItemById('TEXTFIELD').value.subscribe(function(textInput){
       var revertedInput = textInput.split('').reverse().join(''); //reverse input: 'abc' -> 'cba'
       form.getItemById('REVERT_INPUT').setValue(revertedInput);
   });
});

Other Recipes in this category: