Welcome, Guest
Username: Password: Remember me

TOPIC: Randomly Select From A Conditional List

Randomly Select From A Conditional List 1 year 2 months ago #92576

  • factor0
  • factor0's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Hi all,

I need help! I've been wrestling with this for the past few days and have exhausted the existing forum threads searching for an answer, so I figured I would just ask. The .lss file is attached.

Here's the scenario:

1) A respondent would see 10 brands, and would rank familiarity with those brands on a 5 point scale.
2) For those responses with a score of '5', I have moved them into a radio list (using the information from this thread

Where I'm stuck:

I now want to randomly select an option from Page 2 to take forward in the survey for brand ratings. See page 3 for the piping to get an idea of how it would look. The only problem is that I'm not sure I can randomly select.

Ideally I could replicate this for two unique brands (perhaps a multi-punch question), but the javascript has been volatile when I try to change the question type and I have a very minimal understanding of Javascript.

Has anyone worked through this before, or have any suggestions?

Thanks so much,
Mike
Attachments:
The administrator has disabled public write access.

Randomly Select From A Conditional List 1 year 2 months ago #92604

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
I'm not sure I understand the second requirement but to store a random brand from page 2 and pipe it to page 3, you can do this...

1) Add and manipulate the hidden multi-options question in page 1 as you have already done. Here is updated script that, when inserted into the source of the array, will automatically detect the question IDs and handle the manipulation.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Identify the questions
		var q1ID = '{QID}';
		var q1 = $('#question'+q1ID+'');
		var q2 = $(q1).nextAll('.multiple-opt:eq(0)');
		var q2ID = $(q2).attr('id').split('question')[1];
 
		// Hide the hidden question
		$('#question'+q2ID+'').hide();
 
		// Assign a class to all "high score" array radio buttons
		$('#question'+q1ID+' table.question tbody tr').each(function(i) {
			$('input.radio', this).each(function(i) {
				if (i > 3) {
					$(this).addClass('highScore');
				}
			});
		});
 
		// Interrupt next/submit function
		$('form#limesurvey').submit(function(){
 
			// Reset the hidden question
			$('#question'+q2ID+' input.checkbox').attr('checked', false);
 
			// Check the appropriate boxes of the hidden question
			$('#question'+q1ID+' table.question tbody tr').each(function(i) {
				if($('.highScore:checked', this).length > 0) {
					$('#question'+q2ID+' li:eq('+i+') input.checkbox').attr('checked', true);
				}
			});
 
			// Carry on with submit
			return true;
		});
	});
 
</script>

2) Add a short-text question to page 2 (lets call it randomBrand). Insert this script in the source of the radio on page 2. The script will hide the short-text and then load it with a random visible brand.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Identify the questions
		var q1ID = '{QID}';
		var q1 = $('#question'+q1ID+'');
		var q2 = $(q1).nextAll('.text-short:eq(0)');
		var q2ID = $(q2).attr('id').split('question')[1];
 
		// Hide the hidden question
		$('#question'+q2ID+'').hide();
 
		var brands = new Array();
 
		// Create an array of all visible brands
		$('#question'+q1ID+' .answertext:visible').each(function(i) {
			brands.push($(this).text());
		});
 
		//Load the hidden question with a random brand from the array
		var randNum = Math.floor(Math.random()*brands.length);		
		$('#question'+q2ID+' input.text').val(brands[randNum]);
	});
 
</script>

3) Pipe the short-text value into page 3 with something like:
{randomBrand.shown}

Here's a copy of the survey back with the scripts in the source of the first questions on page 1 and 2.

File Attachment:

File Name: limesurvey_survey_995223.lss
File Size: 36 KB
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.
Last Edit: 1 year 2 months ago by tpartner.
The administrator has disabled public write access.

Randomly Select From A Conditional List 1 year 2 months ago #92614

  • factor0
  • factor0's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Hi Tony,

Wow, thank you very much.

The second requirement was basically to repeat the random variable creation for a second brand (i.e. instead of piping one random brand into a single followup question, pipe two different random brands into two separate questions).

Is it possible to to replicate it for a second variable by changing the q2id to a different variable id, and adding a second instance of the Page 2 script? It looks like that would override the original code, as I can't see how to change the {randomBrand.shown} variable into two separate variables (i.e.):

{randomBrand1.shown}
{randomBrand2.shown}

This has definitely been a learning process!

Best,
Mike
The administrator has disabled public write access.

Randomly Select From A Conditional List 1 year 2 months ago #92641

  • factor0
  • factor0's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Just a quick update for the sake of posterity. I got it to work with two separate questions (both being randomly selected from the same list).

Revised file is attached in case anyone runs into the same issue.

Two questions that are probably less tricky, but I couldn't find direct answers to:

1) If I hide page 2 completely (using the Limesurvey options), then my randomly selected choices do not pipe through to page 3. As such, I have the page autopunching submit using the code below, however it has a bit of lag and isn't that seamless from a survey takers standpoint.
jQuery(document).ready(function($) {
document.limesurvey.move.value = 'movenext';
document.limesurvey.submit(); });
</script>

2) Does anyone have some code snippet that basically tells the program "If random variable 2 is the same as random variable 1, please re-select variable 2."
Attachments:
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.174 seconds
Donation Image