- Posts: 6
- Thank you received: 1
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
prefill questions based on attribute's in the token table
- joosts
- Topic Author
- Offline
- New Member
How to use attributes defined in the token table to fill in questions.
1. You need to know how to activate and use Javascript in Limesurvey
If you want to use javascript within Limesurvey you have to disable a security filter before you can add your javascript in source code mode.
* Go to global settings and set the setting 'XSS-Filter' to 'off'.
* Add a new question
* In the question field, expande the tool bar by clicking the upper grey bar to get to the source code editor:
* Click on the button LimeFitWin to get full screen mode with even more buttons in the tool bar:
* Click on the button Source
Now you can add your code. A simple test to see if javascript is enabled is to echo an alert. Use this code to test:
<script type="text/javascript" charset="utf-8">
alert("Test!");
</script>
2. You need to be able to load info in the token table. I just exported an existing table to understand the structure, than used CONCAT in excel to create the text for a CSV, which I pasted in Notepad to save as .CSV .
In recent versions of Limesurvey, you can create unlimited token fields. These have 'nicknames' and internal names. I could only make it work if i used the Token table editor to create the needed fields, then import the CSV file. Extra token fields have to be referred to in this form TOKEN:ATTRIBUTE_X , not using their nickname.
a. To fill in an open text field question I pasted this script into the source code of the question text:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { document.getElementById("answer74931X156X880").value = '{TOKEN:LASTNAME}' }); </script>
Save and reopen the question to check if the code hasn't been messed with. This can happen if you haven't followed the steps above to activate Javascript, or when pasting from another program.
Where 74931 is the survey ID, 156 the group ID and 880 the question ID. Of course, you can use any token. For example TOKEN:ATTRIBUTE_45 .
b. For a single choice question, I used the script below. Here the IF statement refers to a certain individual property, which in this case is a text. It's interesting to know that the syntax does not change if your attribute is text or integer.
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { if('{TOKEN:ATTRIBUTE_2}' == 'VZW') {$("input#answer74931X156X885X1").attr('checked', true);} if('{TOKEN:ATTRIBUTE_2}' == 'FV') {$("input#answer74931X156X885X2").attr('checked', true);} if('{TOKEN:ATTRIBUTE_2}' == 'BVBA') {$("input#answer74931X156X885X3").attr('checked', true);} if('{TOKEN:ATTRIBUTE_2}' == 'NG') {$("input#answer74931X156X885X4").attr('checked', true);} if('{TOKEN:ATTRIBUTE_2}' == 'NV') {$("input#answer74931X156X885X5").attr('checked', true);} if('{TOKEN:ATTRIBUTE_2}' == 'CommV') {$("input#answer74931X156X885X6").attr('checked', true);} if('{TOKEN:ATTRIBUTE_2}' == 'Eenmanszaak') {$("input#answer74931X156X885X7").attr('checked', true);} }); </script>
I use X1 through X7 to define answer options. You choose this yourself when defining answer option codes. Be careful, as this info is case sensitive.
c. This syntax would work just as well for a multi answer question, if it weren't that of course you cannot define more than one answer in just one attribute. Hence you need at least as much attribute columns in your token table as the user with the biggest number of crossed answer options had in your original table.
In this case, attribute 3 through 5 contain the old answers. For example 21 could refer to apples, 22 to pears. The answer code for 'apples' is X21, for 'pears' X22. So if a person has the value 21 on either of the three attributes, answer option X21 will be marked.
if('{TOKEN:ATTRIBUTE_3}' == '21' || '{TOKEN:ATTRIBUTE_4}' == '21' || '{TOKEN:ATTRIBUTE_5}' == '21') {$("input#answer74931X156X884X21").attr('checked', true);} if('{TOKEN:ATTRIBUTE_3}' == '22' || '{TOKEN:ATTRIBUTE_4}' == '22' || '{TOKEN:ATTRIBUTE_5}' == '22') {$("input#answer74931X156X884X22").attr('checked', true);} if('{TOKEN:ATTRIBUTE_3}' == '23' || '{TOKEN:ATTRIBUTE_4}' == '23' || '{TOKEN:ATTRIBUTE_5}' == '23') {$("input#answer74931X156X884X23").attr('checked', true);} if('{TOKEN:ATTRIBUTE_3}' == '24' || '{TOKEN:ATTRIBUTE_4}' == '24' || '{TOKEN:ATTRIBUTE_5}' == '24') {$("input#answer74931X156X884X24").attr('checked', true);}
I used this principle on a question with 70 answer options and 14 token attributes, and it worked just fine.
- duvemyster
- Offline
- Senior Member
- Posts: 60
- Thank you received: 6
(Should this approach from manual.limesurvey.org/QS:Defaults work in Version 2.00+ Build 130802? -- "In the text entry field for the default value, you would enter {TOKEN:ATTRIBUTE_1}")
- Mazi
- Offline
- Official LimeSurvey Partner
Pre-filling a text field at Limesurvey 1.92 using default answers and Expression Manager
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
- duvemyster
- Offline
- Senior Member
- Posts: 60
- Thank you received: 6
Should entering {TOKEN:ATTRIBUTE_1} in a default field of a multiple short-text question be working?
- Mazi
- Offline
- Official LimeSurvey Partner
Just give it a try...duvemyster wrote: Should entering {TOKEN:ATTRIBUTE_1} in a default field of a multiple short-text question be working?
...the answer is "yes".
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
- duvemyster
- Offline
- Senior Member
- Posts: 60
- Thank you received: 6
I haven't figured out yet what I was doing differently yesterday, though this has been working perfectly for me each time today.
- Anana
- Offline
- New Member
- Posts: 8
- Thank you received: 0
Thank you for your input on this question, I am using your code to pre-fill a limewurvey questionnaire using the last version of lime survey, and it works perfectly!!
However, I have an issue pre-filling a single choice question, but uisng a dropdown list instead of a radio list. The code provided on this forum is not working as soon as we select a dropdown format. As I have some long list (e.g countries code) to fill in, it would be very convenient to use a dropdown.
Did you by any chance already encounter the same issue? Or would you have a solution to pre-fill a dropdown? I haven't found any by far.
Thanks in advance and all the best!
- Mazi
- Offline
- Official LimeSurvey Partner
2. Please post a link to a short sample survey.
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
- Anana
- Offline
- New Member
- Posts: 8
- Thank you received: 0
1. We never tried it for drop downs at previous versions, we did it here for the first time.
2. For instance, we want to create a drop down list of 4 choices for the respondents, asking them to select the role of their institution (NSO/ Academi/ Private sector, IO). See excel sheet. The code works perfectly for pre-filling a radio list, but does not prefill for dropdown. I am using the code below:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
if('{TOKEN:ATTRIBUTE_1}' == 'NSO') $("input#answer38934X2077X29244A1").attr('checked', true);
if('{TOKEN:ATTRIBUTE_1}' == 'Academia') $("input#answer38934X2077X29244A2").attr('checked', true);
if('{TOKEN:ATTRIBUTE_1}' == 'Private Sector') $("input#answer38934X2077X29244A3").attr('checked', true);
if('{TOKEN:ATTRIBUTE_1}' == 'IO') $("input#answer38934X2077X29244A4").attr('checked', true);
});
</script>
With A1 to A4 being the corresponding answer options for this question.
- tpartner
- Offline
- LimeSurvey Community Team
- Posts: 10634
- Thank you received: 3698
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { if('{TOKEN:ATTRIBUTE_1}' == 'NSO' && $('#answer38934X2077X29244').val() == '') { $('#answer38934X2077X29244 option').removeAttr('selected'); $('#answer38934X2077X29244 option[value="A1"]').attr('selected', 'selected'); $('#answer38934X2077X29244').val('A1'); } if('{TOKEN:ATTRIBUTE_1}' == 'Academia' && $('#answer38934X2077X29244').val() == '') { $('#answer38934X2077X29244 option').removeAttr('selected'); $('#answer38934X2077X29244 option[value="A2"]').attr('selected', 'selected'); $('#answer38934X2077X29244').val('A2'); } if('{TOKEN:ATTRIBUTE_1}' == 'Private Sector' && $('#answer38934X2077X29244').val() == '') { $('#answer38934X2077X29244 option').removeAttr('selected'); $('#answer38934X2077X29244 option[value="A3"]').attr('selected', 'selected'); $('#answer38934X2077X29244').val('A3'); } if('{TOKEN:ATTRIBUTE_1}' == 'IO' && $('#answer38934X2077X29244').val() == '') { $('#answer38934X2077X29244 option').removeAttr('selected'); $('#answer38934X2077X29244 option[value="A4"]').attr('selected', 'selected'); $('#answer38934X2077X29244').val('A4'); } }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
- Anana
- Offline
- New Member
- Posts: 8
- Thank you received: 0
Cheers
- akankashasharma91
- Offline
- New Member
- Posts: 1
- Thank you received: 0
I require an urgent help in case of an anonymous survey i want a token field value to be stored in a question which will be of equation type. But i am not able to do it using the above mentioned solution.
Please help.
Thanks in advance.