How to use JavaScript in a questionnaire
Research Studio has three JavaScript functions that can be modified to allow users to program extra functionality into questionnaires. These scripts can be added to the questionnaire by adding the HTML element.
pageReady()
This function is called when the questionnaire page has completed loading. It can be used to run animations or videos once all the elements have been loaded onto the screen.
This script disables the answer and next buttons for 10 seconds while a video is playing.
validate()
A function called when the Previous or Next buttons are pressed. This function can be used to run complex validation scripts.
This script loops through a number of numeric rows on the screen and only allows the user to continue if the total equals 100.
A variable on the page indicates if the "next" or the "previous" button was pressed. Typically one might only want to validate moving forward. Use:
$('#mi_validate').val() to get the value. It is "1" if moving forward, and "-1! if moving backward.
Return "true" if validation is ok, the system will automatically display the next/previous question, or "false" and the respondent will stay on the current question.
globalPageReady()
This function can be placed in an "imbeded" script-file, and will fire, whenever a page is loaded.
iQuestFire()
In addition to pageReady/globalPageReady and validate, if iQuests are used on the page (interactive questions), the function iQuestFire() is called inside of the iQuest.
Sequence:
The scripts are called in the following sequence:
1) globalPageReady
2) iQuestFire
3) pageReady
Note:
On any page (question or info) there can only be one version of any of these functions. If more than one is defined, only the last one will be executed.
Since there is only one iQuestFire called, it is only supported to display one question at a time using iQuests. If displaying more than one question on a page, be aware of the limitations, e.g. a validate() function must then validate all displayed questions, pageReady() must in case also handle all relevant questions on the page, in the same routine.
jQuery
Research Studio has the jQuery JavaScript library available.
“jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.”
If you are familiar with using jQuery you can use the selectors and functions directly within scripts without loading the libraries in your script.
Further information on jQuery can be found at: http://jquery.com/ and http://jqueryui.com/