- Posts: 30
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Prefill answers in a radio list - problem with columns
- arpsh
- Topic Author
- Offline
- Junior Member
Less
More
10 years 5 months ago #100842
by arpsh
Prefill answers in a radio list - problem with columns was created by arpsh
Hi folks
This forum has been a great resource for me to work out how to do various things (in particular I have gratefully been using various workarounds originally posted by tpartner), but I have one that has got me stumped. I will admit, my aptitude with code is typically educated guesswork based on prior examples, rather than any underlying knowledge
Anyway, based upon previous forum posts I have myself a working piece of code that prefills a standard radio list question, as can be seen below (adapted from help tpartner has provided to others). This code prefills a specific radio button unless a button has already been checked. This latter part of the code ensures that if the participant overides the prefilled choice, their choice doesn't get overwritten if they navigate back. BTW QQ needs to be replaced by the question code, and SGQA with the SGQA code.
My problem is that this code breaks if the radio list is presented in 2 or more columns, evidently because it needs to reference a different element when looping through. However, I can't work out how to reference the columns when looping through in order to make the code work in that scenario.
I suspect it is a simple fix, I just can't work it out myself so any help would be very much appreciated!
Thanks
This forum has been a great resource for me to work out how to do various things (in particular I have gratefully been using various workarounds originally posted by tpartner), but I have one that has got me stumped. I will admit, my aptitude with code is typically educated guesswork based on prior examples, rather than any underlying knowledge
Anyway, based upon previous forum posts I have myself a working piece of code that prefills a standard radio list question, as can be seen below (adapted from help tpartner has provided to others). This code prefills a specific radio button unless a button has already been checked. This latter part of the code ensures that if the participant overides the prefilled choice, their choice doesn't get overwritten if they navigate back. BTW QQ needs to be replaced by the question code, and SGQA with the SGQA code.
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ var qID = QQ; var answerID = 'input#answerSGQA'; // Loop through the radios and check the referenced radio // if none are already checked $('#question'+qID+' ul.answers-list ').each(function(i) { if($('input.radio:checked', this).length == 0) { $(answerID).attr('checked', true); } }); }); </script>
My problem is that this code breaks if the radio list is presented in 2 or more columns, evidently because it needs to reference a different element when looping through. However, I can't work out how to reference the columns when looping through in order to make the code work in that scenario.
I suspect it is a simple fix, I just can't work it out myself so any help would be very much appreciated!
Thanks
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10612
- Thank you received: 3691
10 years 4 months ago #100899
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Prefill answers in a radio list - problem with columns
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
- arpsh
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 30
- Thank you received: 0
10 years 4 months ago #100917
by arpsh
Ah, my fault. I simplified the code to make my example easier to follow, but in doing so obviously made it less clear. In my actual working survey I vary the radio that is prefilled based upon the answer to an earlier equation question. The code that I simplified/removed in the previous example is below - variable typeID pulls in the answer from the previous equation question {Q10} using expression manager, then this is added to the SGQ in the variable answerID to form a full SGQA reference to prefill the appropriate radio in the current question.
I can make this work on one or the other column of a 2 column radio list by referencing ul.first or ul.last rather than ul.answers-list, but I can't make them work together. It may be that I have to have some form of nested loop, but I can't work out how without some similar code that I can crib off!
Replied by arpsh on topic Prefill answers in a radio list - problem with columns
tpartner wrote: Can you not use a "default answer" setting?
Ah, my fault. I simplified the code to make my example easier to follow, but in doing so obviously made it less clear. In my actual working survey I vary the radio that is prefilled based upon the answer to an earlier equation question. The code that I simplified/removed in the previous example is below - variable typeID pulls in the answer from the previous equation question {Q10} using expression manager, then this is added to the SGQ in the variable answerID to form a full SGQA reference to prefill the appropriate radio in the current question.
Code:
var typeID ={Q10}; var answerID = 'input#answerSGQ'+typeID;
I can make this work on one or the other column of a 2 column radio list by referencing ul.first or ul.last rather than ul.answers-list, but I can't make them work together. It may be that I have to have some form of nested loop, but I can't work out how without some similar code that I can crib off!
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10612
- Thank you received: 3691
10 years 4 months ago #100919
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Prefill answers in a radio list - problem with columns
Hmm...I don't think you need a loop at all. Try this (the {QID} placeholder will return the parent question ID)
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ var qID = {QID}; var typeID ={Q10}; var answerID = 'input#answerSGQ'+typeID; if($('#question'+qID+' input.radio:checked').length == 0) { $(answerID).trigger('click'); } }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: arpsh
The topic has been locked.
- arpsh
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 30
- Thank you received: 0
10 years 4 months ago #100922
by arpsh
Replied by arpsh on topic Prefill answers in a radio list - problem with columns
Bingo
I thought there was some straightforward way to do it, but educated quesswork can only get me so far! A great many thanks Tony, including a massively quick response to my post!
I thought there was some straightforward way to do it, but educated quesswork can only get me so far! A great many thanks Tony, including a massively quick response to my post!
The topic has been locked.