Prefill DropDown List

More
4 years 7 months ago #87112 by nasprok
nasprok created the topic: Prefill DropDown List
Last year we conducted a survey and everything worked perfectly with some questions that we wanted to be prefilled from the token table. I'm trying to do something similar now (I have upgraded to the latest version of LimeSurvey) and strange things happens. When I get the pre-selection of a dropdown list to work with the following but not elegant code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
if('{TOKEN:ATTRIBUTE_3}' == '3') { 
if('{TOKEN:ATTRIBUTE_4}' == '1') { document.getElementById("answer46981X97X2311").value = 1 }
if('{TOKEN:ATTRIBUTE_4}' == '2') { document.getElementById("answer46981X97X2311").value = 2 }
if('{TOKEN:ATTRIBUTE_4}' == '3') { document.getElementById("answer46981X97X2311").value = 3 }
if('{TOKEN:ATTRIBUTE_4}' == '4') { document.getElementById("answer46981X97X2311").value = 4 }
if('{TOKEN:ATTRIBUTE_4}' == '5') { document.getElementById("answer46981X97X2311").value = 5 }
if('{TOKEN:ATTRIBUTE_4}' == '6') { document.getElementById("answer46981X97X2311").value = 6 }
}
});
</script>

and the particular question is mandatory when I press next to see the rest of the questions I get a message telling me that the question is mandatory and that I haven't selected anything.
When I select that the question is not mandatory although I see the dropdown menu with the selected value, that value is not recorded in the results. I' m trying to solve this several hours now and I'm unable to figure it out. Any help would be really appreciated because I have a deadline till Monday.

Please Log in to join the conversation.

More
4 years 7 months ago #87124 by DenisChenu
DenisChenu replied the topic: Prefill DropDown List
What question type you use ?

Simple dropdown list ?
Because i just test with a List (dropdown)
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#answer{self.SGQA}").val("A1");
 });
</script>
mandatory question and it work.

Denis
</script>

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
4 years 7 months ago - 4 years 7 months ago #87128 by nasprok
nasprok replied the topic: Prefill DropDown List
I think I narrowed it down a little. What I want to do:
1)Prefill a List (radio) with 3 options from a Token attribute. That works fine with the following code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
    if('{TOKEN:ATTRIBUTE_3}' == '1') {
        $('input#answer46981X97X23101').attr('checked', true);
    }
    if('{TOKEN:ATTRIBUTE_3}' == '2') {
        $('input#answer46981X97X23102').attr('checked', true);            
    }
    if('{TOKEN:ATTRIBUTE_3}' == '3') {
        $('input#answer46981X97X23103').attr('checked', true);
    } 
2) In the code of the same question I added the following so the next 3 questions (List (dropdown)) to hide/show based on the previous prefill.(if i dont do this form loads and none of the 3 questions shows. Even with conditions)
if(document.getElementById('answer46981X97X23101').checked)
{
   document.getElementById('question2307').style.display='block'; 
   document.getElementById('question2308').style.display='none';
   document.getElementById('question2311').style.display='none';
}
if(document.getElementById('answer46981X97X23102').checked)
{
   document.getElementById('question2307').style.display='none'; 
   document.getElementById('question2308').style.display='block';
   document.getElementById('question2311').style.display='none';
}
if(document.getElementById('answer46981X97X23103').checked)
{
   document.getElementById('question2307').style.display='none'; 
   document.getElementById('question2308').style.display='none';
   document.getElementById('question2311').style.display='block';
}
 
});
</script>
3) Those 3 questions should also prefill based on another Token attribute so I used the code in my original post. The other two questions have similar scripts. This also works!
4) Here is where the headache begins. I also want to set a condition for each of the 3 questions based on the selection of the List (radio). The conditional show/hide works fine but when I submit the survey the data of the answer selected is not saved in the database. When it works? If I just click the dropdown even I select the same answer it shows in the database. Also when I remove the conditions.

The whole idea so you understand what I'm trying to do is: Our university has 3 departments (List (radio)) and each department has several sections (List (dropdown)). So I want to prefill the department and the section of each participant but to also give them the option to change those selections if they transferred from one department to another since I uploaded their data in the tokens table.

Sorry for the long post and if I didn't give you all the info in my first post. I would really appreciate you help! ;)
Last Edit: 4 years 7 months ago by nasprok. Reason: addition

Please Log in to join the conversation.

More
4 years 7 months ago #87135 by DenisChenu
DenisChenu replied the topic: Prefill DropDown List
Hello,

Maybe the best is:
- Make a "demonstration little survey" with just one group for the 2 question.
- Export this lss file, and maybe a csv token file (false information)

We can import survey and token to test and to adat the code.

Denis
PS: jquery is really best than document.getElementById . To hav the question ID, you can use {CODE.qid} look at Expression Manager documentation.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
4 years 7 months ago #87137 by nasprok
nasprok replied the topic: Prefill DropDown List
Thank you very much for the response and the advice. I attached the files you asked me. I' m really curious what's you opinion on the problem.
Attachments:

Please Log in to join the conversation.

More
4 years 7 months ago #87357 by nasprok
nasprok replied the topic: Prefill DropDown List
Any feedback?

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now