JavaScript in questions
JavaScript changes the survey while participants take it: it can make a field read-only, react to an answer or add a widget. Add a script to the text of a question, and it runs on the page where that question is. The survey pages load jQuery, so you can use it in your scripts.
To run JavaScript on all pages of your surveys, put it in custom.js of your theme (see Adding your own CSS and JavaScript). The file is part of the theme, so its code runs in every survey that uses the theme.
Who may add scripts
Superadministrators can always add scripts. For other administrators, two settings under Configuration ⇒ Global settings ⇒ Security decide:
- Filter HTML for XSS (default: On): removes scripts and other suspicious HTML from the texts these administrators save, such as question, help and group texts. It never applies to superadministrators. To let other administrators add scripts to question texts, turn it off.
- Restrict question script for XSS restricted user (default: On): applies to the Script field of the classic editor (see below). When it is On, administrators whose texts are filtered can't change that field. When it is Off, they can, even with the filter on.
Turn these settings off only if you trust everyone who can edit surveys: scripts run in your participants' browsers.
In config.php, filterxsshtml_forcedall forces the XSS filter for all users, superadministrators included, and filterxsshtml_enablescript decides who may still add scripts. The two settings are then shown as read-only in the global settings.
Add a script to a question
New editor:
- Click the text of a question on the page where the script should run. The text toolbar appears.
- Click ⋮ and then Edit as HTML. The window Edit question using HTML opens.
- Add your script below the text, between
<script>and</script>(1). Put your code inside$(document).on('ready pjax:complete', function() { ... });so that it runs once the page is ready. - Click Save (2).
- Click the eye icon in the top bar and check the script in the preview. Scripts don't run in the editor.
The script in the picture makes the answer field of question 375 read-only.
Classic editor: open the question, click the Source button in the toolbar of the question text, add the script below the text and click Save. Check it with Preview question or Preview survey.
The classic editor also has a Script tab next to Question and Help. Code in this field needs no <script> tags: LimeSurvey wraps it so that it runs after the question is displayed. You can hide the tab with Show script field in your personal settings.
Keep curly brackets apart
ExpressionScript reads text in curly brackets as an expression. In a script in a question text, leave a space or a line break after every { and before every }:
- Works:
if (x) { doSomething(); } - Fails:
if (x) {doSomething();}
{QID} is an exception you can use on purpose: LimeSurvey replaces it with the ID of the question, so #question{QID} always points to the question the script is in. See ExpressionScript Engine - Quick start guide.
For ready-made scripts, see JavaScript examples you can copy and Workarounds: Manipulating a survey at runtime using Javascript. Many tasks that used to need JavaScript are settings now: array filters, exclusive options, sums and minimum dates, for example. See Workarounds.
FAQ
Why can't a colleague add a script?
Their texts are filtered by Filter HTML for XSS under Configuration ⇒ Global settings ⇒ Security, so their scripts are removed when they save. A superadministrator can add the script for them or turn the filter off (see Who may add scripts).
My script doesn't run. What do I check?
Test in the preview, not in the editor. Check the curly brackets, check that the script is on the same page as the elements it changes, and open the console of your browser for error messages. If the script disappeared after saving, your user may not add scripts (see Who may add scripts).