Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Prefill array (radio)

  • lwolters
  • lwolters's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144333 by lwolters
Prefill array (radio) was created by lwolters
Hi,

I have an array that's used as an example-question. Therefore I need to prefill the answers.
SID: 855275
GID: 6068
QID: 50438

I have two subquestions (SQ001 and SQ002) and four answer-options (coded 0,1,2,3,4).
The answer on SQ001 needs to be the option coded 1, and the answer on SQ002 the option coded 2.

I tried this:
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#answer855275X6068X50438SQ001_1').attr('checked',true);
 $('#answer855275X6068X50438SQ002_2').attr('checked',true);
</script>

That doesn't work.
Can anyone help me with this?
Thnx!

Liselot
The topic has been locked.
  • lwolters
  • lwolters's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144334 by lwolters
Replied by lwolters on topic Prefill array (radio)
I missed a closing accolade in the code above, but that's not the solution.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago - 7 years 4 months ago #144335 by Joffm
Replied by Joffm on topic Prefill array (radio)
Hi, Liselot,

why the Javascript?
It's easily done by an equation.
Before your question (e.g. with code Q1) insert an equation.
In the question text insert:
{Q1_SQ001=1}
{Q1_SQ002=2}

That's it.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 7 years 4 months ago by Joffm. Reason: missing salutation
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago #144345 by tpartner
Replied by tpartner on topic Prefill array (radio)
It seems to me that both of those approaches will reset the question to the default values when returning to the group.

I would do this with JavaScript:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {    
 
    // Default answer for Row "SQ001"
    if($('#question{QID} .answers-list[id$="SQ001"] input.radio:checked').length == 0) {
      $('#question{QID} input.radio[id$="SQ001-1"]').trigger('click');
    }  
 
    // Default answer for Row "SQ002"
    if($('#question{QID} .answers-list[id$="SQ002"] input.radio:checked').length == 0) {
      $('#question{QID} input.radio[id$="SQ002-2"]').trigger('click');
    }
    });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • lwolters
  • lwolters's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144347 by lwolters
Replied by lwolters on topic Prefill array (radio)
Both thank you very much for your reply. In this case Joffm's solution will do, because the question is only used as an example en the data is not used.

Greetings,
Liselot
The topic has been locked.
More
3 years 11 months ago - 3 years 11 months ago #196100 by hamstr
Replied by hamstr on topic Prefill array (radio)
Sorry for reopening this thread - but I was wondering how I could prefill the rows based on previous answers rather than fixed values (as used here)? To be more specific: I have the same questionnaire embedded twice as an array (in different question groups) and want to transfer the answers of the array to the second one so that participants can see their previous answers and adapt them if necessary. I guess I can use a code similar to the one posted here, but I was not able to find out which parameter to change.

I'd really appreciate your help! Thanks a lot!

tpartner wrote: It seems to me that both of those approaches will reset the question to the default values when returning to the group.

I would do this with JavaScript:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {    
 
    // Default answer for Row "SQ001"
    if($('#question{QID} .answers-list[id$="SQ001"] input.radio:checked').length == 0) {
      $('#question{QID} input.radio[id$="SQ001-1"]').trigger('click');
    }  
 
    // Default answer for Row "SQ002"
    if($('#question{QID} .answers-list[id$="SQ002"] input.radio:checked').length == 0) {
      $('#question{QID} input.radio[id$="SQ002-2"]').trigger('click');
    }
    });
</script>

Last edit: 3 years 11 months ago by hamstr. Reason: include quote
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #196103 by Joffm
Replied by Joffm on topic Prefill array (radio)
Hi,
though I am sre that Tony will provide a javascript solution, here something to do it with EM
Question of type equation after the first array:

{Q2_SQ001=if(is_empty(Q2_SQ001),Q1_SQ001,Q2_SQ001)}
{Q2_SQ002=if(is_empty(Q2_SQ002),Q1_SQ002,Q2_SQ002)}
{Q2_SQ003=if(is_empty(Q2_SQ003),Q1_SQ003,Q2_SQ003)}
...

But: What do you want to happen, if the respondent adapts the second array, then goes back to the first, changes something, and then proceeds again to the second array.
My code avoids that changes are reflected.


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: DenisChenu
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose