Logo Utrecht University

UiL OTS Labs

How-tos

Default answers in LimeSurvey

Last updated on 20 May 2014 by Martijn van der Klis

Introduction 

LimeSurvey has several possibilities to make a certain answer the default; allowing your participant to be able to navigate more quickly through your survey. In this how-to, we’ll try and explain the options. If you happen to stumble across a new variant or just get stuck, please drop lab support a line. 

Via the user interface

defaultanswers

Of course, the easiest way to add default answers is via the user interface. For a range of questions (including text, numerical and date inputs), LimeSurvey allows you to add a default answer using the default answers-button.

You can also use results from previous questions here, using the question codes in curly brackets. A kinda silly example would be to have questions for first and last name (coded first_name and last_name) and then have a question for the full name, that defaults to {first_name} {last_name}. This latter question has to be in a different question group for this to work. 

Another note: if you want to have a default date value, make sure to use the ISO8601 standard, so YYYY-MM-DD. Pro-tip: you might as well use this format for any date; it’s the most unambiguous. 

Via JavaScript

LimeSurvey allows JavaScript to be added to your question texts. You can add JavaScript when you switch to the “source mode”, in which you’ll see the plain HTML instead of the marked-up text. You can use JavaScript to set default answers. For example, for a simple text question, you could set the default answer via JavaScript as well (replace answerXXX with your actual question code, something like answer65377X365X4259, and test with something more meaningful): 

<script type="text/javascript">
$(document).ready(function() {
$('#answerXXX').val('test');
});
</script>

Note that this script uses jQuery instead of standard JavaScript, which allows you to use shorter syntax that should work across all major browsers (but still, testing never hurts). And also note that in this particular example you had rather set the default answer via the user interface. But for questions where this option isn’t available, you will have to use jQuery; as is the case with e.g. array questions. Suppose we have a 5-point-choice array and we would like the middle option to be set as a default for all subquestions. We can use a script like the following then: 

<script type="text/javascript">
$(document).ready(function () {
col = 3;
$('input[type=radio][id$=' + col + ']', $('#questionXXX')).each(function () {
$(this).attr('checked', true);
});
});
</script>

This script loops over all inputs of questionXXX of the radio type and that have their id end in ‘3’, and sets their checked value to true. Be sure to replace questionXXX with your question ID. Want to set the fifth answer as default? No sweat, just change the col variable to 5. 

Via tokens

If you have your survey set up to use tokens, you can use the values of these tokens to set default answers in your survey. For example, suppose you received a list of participants with their date of birth and gender, but you would like your survey entrants to check these personal details. 

token_manageIn the token management, you can first add the date of birth and gender as attributes of your token. Click Manage additional attribute fields for that, and add the two attributes. Make sure to save the settings and upload your participant file. 

In the survey, you can then set the default answers for the date of birth question by setting {TOKEN:ATTRIBUTE_1} as the default answer. For the gender question, we’ll have to user JavaScript again. Your JavaScript for your question will have to look something like this (replace answerXXX with your actual question code, something like answer65377X365X4259): 

<script type="text/javascript">
$(document).ready(function() {
$('input#answerXXX' + '{TOKEN:ATTRIBUTE_2}').attr('checked', true);
});
</script>