In interactive form designs, it is common practice to have one or more sections in the form that are not displayed until the user selects the option to include it. With LiveCycle Designer ES, you can add a button along with a script that dynamically adds or removes a particular section (subform) from the form when the user clicks a button.
For example, in the sample interactive Purchase Order, the user can click the Add Comments button to display the Comments section (comments subform). The button has one of two alternating captions, Add Comments or Clear Comments, depending on the current state of the subform. Each time the user clicks the button, the script checks whether the comments subform is displayed and then updates the button caption accordingly.
The button triggers a script that uses instanceManager, the XML Form Object Model object that manages the instance creation, removal, and movement of form objects. When the end user deletes the Comment subform, the instanceManager object removes the subform from both the Form data document object model (DOM) and the Data DOM.
Note that instanceManager uses four methods: addInstance, removeInstance, moveInstance, and setInstances. The naming convention of an instanceManager is the subform name prefixed with an underscore (_subformname). The syntax for instanceManager is _subformname.methodname().
In the sample interactive Purchase Order form, the form author typed the following JavaScript script in the Script Editor using the setInstances method to add and remove the comments subform and change the button’s caption. Notice that the _comments.count == 0 property returns the number of subform instances instantiated.
_comments.setInstances(1); // Add the comments subform.
this.resolveNode(“caption.value.#text”).value =”Clear Comments”; // Change the button’s caption.
}
_comments.setInstances(0); // Remove the comments subform.
this.resolveNode(“caption.value.#text”).value = “Add Comments”; // Change the button’s caption.
}

Creating a button to add and remove a section