Author API Methods
setWidget()
Set the Widget JSON of the Widget (Question or Feature) being edited in an Item.
You would want to use this method so that you can programmatically set up Questions or Features with particular preset attributes, for example.
This method accepts an object specifying a template_reference key for the template as a second argument. If this is set, the Widget is displayed within the template specified by the argument. Use that approach if you have a custom template (with hidden sections, fields, etc.) set up as reference of QuestionType inside question_types.
Examples
var widgetJson = {
"options": [
{ "label": "[Choice A]", "value": "0" },
{ "label": "[Choice B]", "value": "1" },
{ "label": "[Choice C]", "value": "2" }
],
"stimulus": "<p>[This is the stem.]</p>",
"type": "mcq",
"ui_style": {},
"validation": {
"scoring_type": "exactMatch",
"valid_response": {
"score": 1,
"value": [""]
}
}
};
var widgetTemplate = {
"template_reference": "908de244-5c71-4c09-b094-7fb49554f2f9"
};
var hasUpdatedWidget = authorApp.setWidget(widgetJson, widgetTemplate);
console.log(hasUpdatedWidget);
Arguments
-
widgetJson widgetJson
The new Widget JSON to apply.
-
widgetTemplate widgetTemplate
Widget template containing the template reference (case-sensitive)
Return value
Type boolean
Returns true if successful.
Returns false if unable to set the Widget JSON, for example, when not Widget editor view.
Type definitions
-
widgetJson object
Properties of the Widget JSON vary depending on the Question type or Feature type. See Question Types and Features Types in Questions API for more information.
-
widgetTemplate object
The following properties are supported.
-
template_reference string
Widget template reference
-
Related articles
- The
getWidget()method, which allows you to get the Widget JSON of the current Widget.