Welcome, Guest
Username: Password: Remember me

TOPIC: Prefilling Radio button answers at runtime

Prefilling Radio button answers at runtime 3 years 3 months ago #59422

  • danpaul000
  • danpaul000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 9
  • Karma: 0
Hi all,

I am trying to prefill a number of radio button questions at runtime based on answers to previous questions. I am able to work out the logic as to which radio buttons should be selected, but am stuck on the syntax as to how to set a button as selected.

I have seen the other posts to pre-fill numerical input/free text responses, and have been using

<script>
$(document).ready(function() {
var prev_ans = '{INSERTANS:1X1X1}';
document.getElementById("answer1X2X2").value = prev_ans;
});
</script>

to prefill open response. Is the syntax for prefilling radio questions similar? I am using Limesurvey 1.90. Thanks!

Dan
The administrator has disabled public write access.

Re: Prefilling Radio button answers at runtime 3 years 3 months ago #59426

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4050
  • Thank you received: 740
  • Karma: 341
Not quite. Using a couple of jQuery shortcuts:
<script>
	$(document).ready(function() {
		var prev_ans = '{INSERTANS:1X1X1}';
 
		if(prev_ans == 1) {
			$('#answer1X2X11').attr('checked', true);
		}
		else {
			$('#answer1X2X12').attr('checked', true);
		}
	});
</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.
The administrator has disabled public write access.

Re: Prefilling Radio button answers at runtime 3 years 3 months ago #59428

  • danpaul000
  • danpaul000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 9
  • Karma: 0
Hi Tony,

Thanks for the tip, I was able to assign the radio buttons. However, I'm now having some difficulty with the conditional. I have a multiple options question on the previous page and want to pre-set radio buttons on the current page corresponding to the checks from the page before. As in, if checkbox 1 is checked on the first page, then radio question 1 will be set to response 1. If checkbox 1 is NOT checked, then radio question 1 will be set to response 2. Etc for checkbox 2 and radio question 2, and checkbox 3 and radio question 3. Below is the code I am trying to use to select one radio button or the other if the first checkbox is checked or not, however the IF statement is not evaluating to true. Thoughts?

3.
<script>    
  $(document).ready(function() {
 
var CB1 = '#answer1X1XCB1';
var RB1 = '#answer1X2XRB1';
var RB2 = '#answer1X2XRB2';
 
if($(CB1).attr('checked') == true) 
{
	$(RB1).attr('checked', true);
}
else
{
	$(RB2).attr('checked', true);
}
      });
</script>
Last Edit: 3 years 3 months ago by danpaul000.
The administrator has disabled public write access.

Re: Prefilling Radio button answers at runtime 3 years 3 months ago #59431

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4050
  • Thank you received: 740
  • Karma: 341
Each sub-question of a multiple options (checkbox) question needs to be accessed separately using {INSERTANS} with the sub-question code. If checked, the {INSERTANS} tag returns the sub-question value, otherwise it will return "No answer".

So, given the following multiple options question:
Untitled-1_2011-04-11.png


If "Red" were selected, {INSERTANS:96687X70X7741} would return "RED".

So to continue your example, if I had a radio question on a subsequent page with the following IDs:
Untitled-2_2011-04-11.png


The code may be:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		var checkBox1 = '{INSERTANS:96687X70X7741}';
 
		if(checkBox1 == 'Red') {
			$('#answer96687X71X7801').attr('checked', true);
		}
		else {
			$('#answer96687X71X7802').attr('checked', true);
		}
	});
 
</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.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.129 seconds
Donation Image