Wilbert's website at SocSci

> LimeSurvey> Script

limesurvey/script.html 2025-02-24

scripted answer values in Limesurvey

You can automatically insert answers in Limesurvey. There are two ways to do this. The easy waysis using the ExpressionScript and the equation question type. The more complicated and more versatile way is with JavaScript

Equation question type

Make a question of type equation. It is in the Mask questions list. Give the question a Question code, for instance PARTICIPANTNUMBER. Now set the question text:

 15

You have now created a question in your survey that will always have the value 15 as answer. It will show up in your data file. Each participant will have the value 15 set for the column PARTICIPANTNUMBER.

The is not very useful. We can use ExpressionScript to set a more useful value. We can for instance set the question text to

{rand(100000,999999)}

Now each participant will have a unique 6 digit number, how cool is that

Note that the Caution about using Assignment Operator (=) applies to all state changing functions. You should use the same caution (and even more so) for using a random function in an equation type question since you run it multiple times when referring to it in the same page.

Make sure to set the question option other > number only if you want to do calculations with the numbers

Javascript

If you need something more complicated that what is possible with ExpressionScript use JavaScript. You can use values from the expression manager in your JavaScript:

<script>
window.addEventListener("load", function(){
	document.querySelector('#question{QID} input').value=14 + {SID}
})
</script>

Note that we should not use the equation question type now. USe something like short text for instance (it doesn't really matter, but must be able to contain the final result).