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
  • joosts's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
13 years 3 weeks ago #57265 by joosts
For a survey, I need to complete a few questions based on info in the token table. The idea is to load info from an external database to the token table, than use limesurvey to check if these data are correct.

Based on info here,I got it to work for text input questions, for example below:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {

if('{TOKEN:ATTRIBUTE_1}' == '1') {
document.getElementById("answer74931X156X880").value = '{TOKEN:FIRSTNAME}' }
});
</script>


But I failed in both multiple choice and single choice questions.

I tried like this:

if('{TOKEN:ATTRIBUTE_1}' == '1') {
document.getElementById("answer74931X156X885").value = 'A1' }

and like this

if('{TOKEN:ATTRIBUTE_1}' == '1') {
document.getElementById("answer74931X156X884A1").value = Y }

and even

if('{TOKEN:ATTRIBUTE_1}' == '1') {
document.getElementById("input#answer74931X156X884A1").attr('checked', true)}
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
13 years 3 weeks ago #57275 by DenisChenu
Hello,

You don't have to use document.getElementById with jquery, and here you use jquery and pure javascript, i don't think it can work like that.

Try:
Code:
if('{TOKEN:ATTRIBUTE_1}' == '1') {
("input#answer74931X156X884A1").attr('checked', true);
}

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • joosts
  • joosts's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
13 years 3 weeks ago #57287 by joosts
I tried both
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
if('{TOKEN:ATTRIBUTE_1}' == '1') {
("input#answer74931X156X884A1").attr('checked', true);
}
});
</script>
and also just to check
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
("input#answer74931X156X884A1").attr('checked', true);
});
</script>

and neither of them worked.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
13 years 3 weeks ago #57288 by DenisChenu
Can you post a link to your survey ?

And oups ... the $ ...
Code:
$("input#answer74931X156X884A1").attr('checked', true);

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: joosts
The topic has been locked.
  • joosts
  • joosts's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
13 years 3 weeks ago #57291 by joosts
that dollar did it, for both multiple and single answer option!
merci beaucoup!
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
13 years 3 weeks ago #57306 by Mazi
Joost, to share your knowledge with other users, ca you write a short tutorial how to prefill different question types like text, single choice, multiple choice, ...?

That would be great!

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
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
13 years 3 weeks ago #57359 by DenisChenu

Mazi wrote: Joost, to share your knowledge with other users, ca you write a short tutorial how to prefill different question types like text, single choice, multiple choice, ...?

That would be great!

Maybe make correction to this one:
docs.limesurvey.org/tiki-index.php?page=...nswer_default_value_

update it with jquery :)

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • joosts
  • joosts's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
13 years 3 weeks ago #57366 by joosts
I'm still working out some details, but I'll write it out soon. I'm making an xls to make programming easier, should I include that file somehow too?
The topic has been locked.
  • joosts
  • joosts's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
13 years 3 weeks ago #57374 by joosts
Still having some trouble. I got everything to work fine for both single and multiple answers. But I have one multi answer question with over 60 answer options. I have it encoded in 12 attributes, each of which can contain one of the 60 answer options.
For example for person Y, attribute_3 may contain 21, which implies answer option X21 on question 884.
The code below does the trick in simple test, though it's not elegant. But when I encode all the attribute fields for all the answer options, Limesurvey freaks and messes with the code. Which is totally rational, given the redundancy of all that code.
Code:
if('{TOKEN:ATTRIBUTE_3}' == '21') {$("input#answer74931X156X884X21").attr('checked', true);}
if('{TOKEN:ATTRIBUTE_3}' == '22') {$("input#answer74931X156X884X22").attr('checked', true);}
if('{TOKEN:ATTRIBUTE_3}' == '23') {$("input#answer74931X156X884X23").attr('checked', true);}
 
if('{TOKEN:ATTRIBUTE_4}' == '21') {$("input#answer74931X156X884X21").attr('checked', true);}
if('{TOKEN:ATTRIBUTE_4}' == '22') {$("input#answer74931X156X884X22").attr('checked', true);}
if('{TOKEN:ATTRIBUTE_4}' == '23') {$("input#answer74931X156X884X23").attr('checked', true);}


I guess it would be better to do it something like this:
Code:
if('{TOKEN:ATTRIBUTE_3}' == '21' OR '{TOKEN:ATTRIBUTE_4}' == '21' OF '{TOKEN:ATTRIBUTE_5}' == '21' OR ...) {$("input#answer74931X156X884X21").attr('checked', true);}

But I can't find the proper JQUERY syntax for OR statements. Any hints?
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
13 years 3 weeks ago #57385 by DenisChenu

joosts wrote: But I can't find the proper JQUERY syntax for OR statements. Any hints?

It's not jquery, but javascript here ;)

OR = ||
Code:
if('{TOKEN:ATTRIBUTE_3}' == '21' || '{TOKEN:ATTRIBUTE_4}' == '21' || '{TOKEN:ATTRIBUTE_5}' == '21' ) {
$("input#answer74931X156X884X21").attr('checked', true);
}

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • joosts
  • joosts's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
13 years 3 weeks ago #57392 by joosts
Here goes:

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:
Code:
<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.
Code:
<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.
Code:
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.
The following user(s) said Thank You: pwmowers
The topic has been locked.
More
10 years 6 months ago - 10 years 6 months ago #99771 by duvemyster
On a questionnaire with token-based response persistence, how would you adjust this approach so that new values entered display in place of the prefilled value from the token table? In other words, how would you make this work like a default value? Or, how would you prefill the default value from the token table instead of prefilling the answer?

(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}")
Last edit: 10 years 6 months ago by duvemyster. Reason: typo
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose