Welcome, Guest
Username: Password: Remember me

TOPIC: Prefill DropDown List

Prefill DropDown List 1 year 11 months ago #87112

  • nasprok
  • nasprok's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 6
  • Karma: 0
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.
The administrator has disabled public write access.

Re: Prefill DropDown List 1 year 11 months ago #87124

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 6358
  • Thank you received: 824
  • Karma: 245
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>
The administrator has disabled public write access.

Re: Prefill DropDown List 1 year 11 months ago #87128

  • nasprok
  • nasprok's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 6
  • Karma: 0
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: 1 year 11 months ago by nasprok. Reason: addition
The administrator has disabled public write access.

Re: Prefill DropDown List 1 year 11 months ago #87135

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 6358
  • Thank you received: 824
  • Karma: 245
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.
The administrator has disabled public write access.

Re: Prefill DropDown List 1 year 11 months ago #87137

  • nasprok
  • nasprok's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 6
  • Karma: 0
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:
The administrator has disabled public write access.

Re: Prefill DropDown List 1 year 11 months ago #87357

  • nasprok
  • nasprok's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 6
  • Karma: 0
Any feedback?
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.161 seconds
Donation Image