jsAPI examples
Accessing data from URL Import
This example shows how to manipulate questions and answers based on URL parameters.
CODE
|
How to add custom JavaScript
If you would like to add custom JavaScript to the form, you need to have access to the account where this form is located.
To add the JavaScript, click the Scripts button on the toolbar menu.
Please contact your account manager or Support team if this button is missing from your ribbon toolbar.
On the Scripts page, you may enter your JavaScript code the following way:
CODE
|
Function start() is executed every time the page loads, allowing programmers to hook up functions to certain events. All internal browser DOM events are accessible. The most common events are button clicks, browser control clicks, or "on blur" events when the focus moves out of the control.
The following fragment of the code illustrates how to hook up a function that will execute every time the 'Next' button is clicked, and function that will execute if 'Next' button is clicked on the page that includes certain DOM document element:
function start()
{
...
// every time Next button is clicked execute function nextPage()
document.getElementById("goNextPage").onclick=nextPage;
// if page contains element QuestionLabelTd6064474 and Next button is clicked execute function nextq6064474()
if (document.getElementById('QuestionLabelTd6064474')!=null){
document.getElementById("goNextPage").onclick=function (){return nextq6064474('QuestionLabelTd6064474');};
}
...
}
jQuery(document).ready(function () {
start();
});
This piece of code shows how to invoke function funcQ15_preprocessor() prior to the load of the page based on the 3D Matrix question Q15:
function start()
{
...
if (vpGetElements('Q15.A1.C1')[0] != null){
funcQ15_preprocessor();
}
...
}
jQuery(document).ready(function () {
start();
});
Also, you may paste the link to the script hosted externally. The FORM naming convention is to add form/survey ID after letter "s" and use .js as a file extension. So in the example below, JavaScript file http://www.worldapp.com/pm-out/s255668.js is stored in FORM production survey ID: 255668.
CODE
|
It can contain a bulk of code like in the following example:
CODE
|
More information about the method used in the example above can be found in JQuery API Documentation.
Send data from one page to another.
On many occasions, when the form/survey consists of multiple pages, there might be a need to execute some logic based on the answers to the previous question(s). The feature called Piping is used to maintain the state across multiple pages.
On the input side of piping, the following JavaScript code needs to be added to the Scripts page of the form/survey:
CODE
|
Consequently, on the receiving end of piping, the following code in start() function is needed in order to read the information and call appropriate logic in functions f_q7() and f_q18() based on it:
CODE
|
Here is another example of piping used to copy address fields from one page of the form/survey to another:
CODE
|