Welcome, Guest
Username: Password: Remember me

TOPIC: Select single radio array button at run-time

Select single radio array button at run-time 3 years 5 months ago #60034

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

I am trying to use js/jquery to recode an answer from a radio button to a radio-array question. I have two radio questions with the same response options followed by the array question on the same page and hidden with JS.

The array has 2 subquestions and the same number of answer options as the radio questions. So if someone selects option 3 for the first radio button and option 4 for the second, the array should fill option 3 for the first sub question and option 4 for the second sub-question.

I have seen the various other posts on setting some default values for all rows in an array, but I can't seem to figure out how to set just one without cycling all the rows.

I have no problem with the conditionals, or the getting the response listener to fire, it is simply what is the syntax for selecting a single option in a single array sub question?

I have tried the following (and some variations thereof) with no success:
 
//Various attempts to set sub-question SQ1 to answer option 3
 
$('input.radio:eq(3)', '#answerSidXGidXQidSQ1').attr('checked', true); 
 
$('input.array:eq(3)', '#answerSidXGidXQidSQ1').attr('checked', true); 
 
$("input#answerSidXGidXQidSQ13").attr('checked', true);
 
$('#questionQidSQ1 input.radio:eq(3)').attr('checked', true);
 
$('#questionQidSQ1 input.array:eq(3)').attr('checked', true);
 

I imagine the solution is something similar, and I am just missing something. Any help would be most excellent.

Cheerio,
Dan
The administrator has disabled public write access.

Re: Select single radio array button at run-time 3 years 5 months ago #60035

  • danpaul000
  • danpaul000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 9
  • Karma: 0
Oh, I have also tried:
 
	$('#answerSidXGidXQidSQ13').attr('checked', true);

Thanks!
D
The administrator has disabled public write access.

Re: Select single radio array button at run-time 3 years 5 months ago #60075

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4271
  • Thank you received: 806
  • Karma: 369
Try this (where "Qid" is the ID of the array question):
$('#questionQid table.question tbody:eq(0) input.radio:eq(2)').attr('checked', true);

This selector finds the third radio button in the first row of an array with ID=Qid. (the indexes for the rows and radio buttons start at 0, so "eq(0)" is the first and "eq(2)" is the third").
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: Select single radio array button at run-time 3 years 5 months ago #60084

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

Thanks for getting back to me but, unfortunately, that line didn't work for me. The chunk of code I am working with is now:
$('#answerSSXGGXQQ1').change(function() {	
	$('#questionQQ2 table.question tbody:eq(0) input.radio:eq(2)').attr('checked', true);
	alert("running");
});

The alert fires so I know the change event triggered. Any other thoughts? Thanks for the assist!

Dan
The administrator has disabled public write access.

Re: Select single radio array button at run-time 3 years 5 months ago #60086

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4271
  • Thank you received: 806
  • Karma: 369
Is the question ID correct? Can you activate a test survey for me to see?
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: Select single radio array button at run-time 3 years 5 months ago #60087

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

I put up a test survey here: chatthreads.com/limesurvey/index.php?sid=66951&lang=en

The SGQs for the three questions are: 66951X3301X16357, 66951X3301X16358, 66951X3301X16359. The full script inside the array question (only set up right now to listen to the first radio question):
Recode:<script type="text/javascript" charset="utf-8">
 
$(document).ready(function(){
//$('#question16359').hide();
 
$('#answer66951X3301X163571').change(function() {	
	$('#question16359 table.question tbody:eq(0) input.radio:eq(0)').attr('checked', true);
	alert("1");
});
$('#answer66951X3301X163572').change(function() {
	$('#question16359 table.question tbody:eq(0) input.radio:eq(1)').attr('checked', true);
	alert("2");
});
$('#answer66951X3301X163573').change(function() {
	$('#question16359 table.question tbody:eq(0) input.radio:eq(2)').attr('checked', true);
	alert("3");
});
$('#answer66951X3301X163574').change(function() {
	$('#question16359 table.question tbody:eq(0) input.radio:eq(3)').attr('checked', true);
	alert("4");
});
 
 
});
</script>

Thanks!
The administrator has disabled public write access.

Re: Select single radio array button at run-time 3 years 5 months ago #60089

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4271
  • Thank you received: 806
  • Karma: 369
Ah, for some reason you have an extra tbody element in the table.

Try this. The selectors now include only tbody elements that have an id that starts with "javatbd".
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
		//$('#question16359').hide();
 
		$('#answer66951X3301X163571').change(function() {	
			$('#question16359 table.question tbody[id^="javatbd"]:eq(0) input.radio:eq(0)').attr('checked', true);
			alert("1");
		});
		$('#answer66951X3301X163572').change(function() {
			$('#question16359 table.question tbody[id^="javatbd"]:eq(0) input.radio:eq(1)').attr('checked', true);
			alert("2");
		});
		$('#answer66951X3301X163573').change(function() {
			$('#question16359 table.question tbody[id^="javatbd"]:eq(0) input.radio:eq(2)').attr('checked', true);
			alert("3");
		});
		$('#answer66951X3301X163574').change(function() {
			$('#question16359 table.question tbody[id^="javatbd"]:eq(0) input.radio:eq(3)').attr('checked', true);
			alert("4");
		});
	});
</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: Select single radio array button at run-time 3 years 5 months ago #60090

  • danpaul000
  • danpaul000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 9
  • Karma: 0
Yes, that did it thank you, Tony!

So, how did you identify the fact that there was an extra tbody element, and what is this element? Thanks again for your help!

Dan
The administrator has disabled public write access.

Re: Select single radio array button at run-time 3 years 5 months ago #60091

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4271
  • Thank you received: 806
  • Karma: 369
Firebug for Firefox. I'm not sure why the extra tbody is there but it seems to have been removed in 1.91.

Capture.GIF
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: Select single radio array button at run-time 3 years 5 months ago #60092

  • danpaul000
  • danpaul000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 9
  • Karma: 0
Wonderful, thank you again!

D
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.317 seconds
Donation Image