Dynamically Change a Button Label
Share
Button controls in Virtual Ticket and Digital Storage Manager provide a powerful method for allowing a user to graphically initiate an action. For example, button controls may be used to print a form, open a related record, etc. The label that appears on the button may simply be a static text value, such as "Print", "Open", etc.
EXAMPLE
In this example, we'll use an integer/checkbox on the form to specify whether an outside purchase has been charged to a job. If it has been charged, the checkbox is "checked" (value = 1), if not, then the checkbox is "unchecked" (value = 0). If the value = 1, then the button label will be set to "View Charge"; if the value = 0, the button label will be set to "Add Charge". This example uses an "objectLoaded" event listener that assesses the value of the checkbox and sets the label accordingly whenever a record object is loaded in the Browser window.
NOTE: This example MetaScript would appear in the Global MetaScript for the form.
| var theButton = Form.getControlById( "btn_OPButton" ); |
| function SetOPButton() |
| { |
| if( Field( 'Checkbox' ) == 1 ) |
| { |
| theButton.label = 'View Charge'; |
| } |
| else |
| { |
| theButton.label = 'Apply Charge'; |
| } |
| } |
The following will set the label of the button when the record is loaded based on the value of the Checkbox field.
| Form.addEventListener( "objectLoaded", function() |
| { |
| SetOPButton(); |
| } |
| ); |
The following watches for any changes made in the form, and when something is changed, it evaluates the Checkbox field and sets the button label accordingly.
| Form.addEventListener( "elementChanged", function() |
| { |
| SetOPButton(); |
| } |
| ); |