Willkommen, Gast
Benutzername: Passwort: Angemeldet bleiben:

THEMA: Prefill answers in a radio list - problem with columns

Prefill answers in a radio list - problem with columns 1 Jahr 5 Tage her #100842

  • arpsh
  • arpshs Avatar
  • OFFLINE
  • Fresh Lemon
  • Beiträge: 11
  • Karma: 0
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.
<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
Der Administrator hat öffentliche Schreibrechte deaktiviert.

Prefill answers in a radio list - problem with columns 1 Jahr 2 Tage her #100899

  • tpartner
  • tpartners Avatar
  • OFFLINE
  • LimeSurvey Team
  • Beiträge: 4271
  • Dank erhalten: 805
  • Karma: 369
Can you not use a "default answer" setting?

h872ddd3.PNG
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Der Administrator hat öffentliche Schreibrechte deaktiviert.

Prefill answers in a radio list - problem with columns 1 Jahr 1 Tag her #100917

  • arpsh
  • arpshs Avatar
  • OFFLINE
  • Fresh Lemon
  • Beiträge: 11
  • Karma: 0
tpartner schrieb:
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.
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!
Der Administrator hat öffentliche Schreibrechte deaktiviert.

Prefill answers in a radio list - problem with columns 1 Jahr 1 Tag her #100919

  • tpartner
  • tpartners Avatar
  • OFFLINE
  • LimeSurvey Team
  • Beiträge: 4271
  • Dank erhalten: 805
  • Karma: 369
Hmm...I don't think you need a loop at all. Try this (the {QID} placeholder will return the parent question ID)
<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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Folgende Benutzer bedankten sich: arpsh

Prefill answers in a radio list - problem with columns 1 Jahr 1 Tag her #100922

  • arpsh
  • arpshs Avatar
  • OFFLINE
  • Fresh Lemon
  • Beiträge: 11
  • Karma: 0
Bingo :cheer:

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!
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Moderatoren: ITEd
Ladezeit der Seite: 0.139 Sekunden
Donation Image