Welcome, Guest
Username: Password:

TOPIC: Hiding multiple choice with comments text box and more

Hiding multiple choice with comments text box and more 4 years 5 months ago #70804

  • uxexp
  • uxexp's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
Hello,
I am using a multiple choice with comments question type. Some of the choices do not require further comments while other do. Can I hide the comments box next to the choices that do not require comments. For exmple:

Q: Choose the websites you have visited in the past:

1. Facebook (no comments)
2. Google (no comments)
3. A work related website [please specify in the comment box]
4. An entertainment website [please specify in the comment box]

Also, is it possible to make the comment piece mandatory for answer choices 3 and 4 (only if selected) in the above example?

Lastly, I would like to capture the first 2 checked labels from the above choices in two separate hidden short text boxes to ask follow-up questions. So for example, if the user chose Google and listed Yahoo Finance for work related website I would like to populate the two short text boxes with "Google" and "Yahoo Finance".

Appreciate your help!

Thanks.
Last Edit: 4 years 5 months ago by uxexp.
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 4 years 5 months ago #70818

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5899
  • Thank you received: 1291
  • Karma: 520
I assume that the "two separate hidden short text boxes" are in a multiple-text question.

Lets call the multiple-options-with-comments question Q1 and the multiple-text question Q2

If Q1 and Q2 are on the same page, you can do this:

1) Set up your survey to use JavaScript.

2) Add the following script to the source of one of the questions. Replace "11" with the Q1 question ID and "22" with the Q2 question ID.

The script does the following:
- Hides the first two text inputs in Q1
- Makes the visible comments mandatory if their associated box is checked
- Loads the text of the first two checked options in Q1 into the text inputs of Q2 (if the option has a visible comment, that is loaded instead)
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() { 
 
		var q1ID = 11;
		var q2ID = 22;
		var msg = 'Please specify in the comment box.';
 
		// Hide the first 2 text inputs of Q1
		$('#question'+q1ID+' li[id^="javatbd"]:eq(0) .comment').hide();
		$('#question'+q1ID+' li[id^="javatbd"]:eq(1) .comment').hide();
 
		// Interrupt next/submit function 
		$('#movenextbtn, #movesubmitbtn').click(function(){
 
			// Reset some stuff
			var failedMandatory = 0;
			$('#question'+q1ID+' input.text').css({'background':''});
			$('#question'+q2ID+' input.text').val('');
 
			// Loop through all check answers in Q1
			$('#question'+q1ID+' input.checkbox:checked').each(function(i){
 
				// Define the row
				var thisRow = $(this).parents('li:eq(0)');
 
				// Check for empty mandatory text inputs
				if($('input.text:visible', thisRow).length > 0 && $('input.text', thisRow).val() == '') {
					$('input.text', thisRow).css({'background':'pink'});
					failedMandatory = 1;
				}
 
				// Load the multiple-text question
				if($('input.text:visible', thisRow).length > 0) {
					$('#question'+q2ID+' input.text:eq('+i+')').val($('input.text', thisRow).val());
				}
				else {
					$('#question'+q2ID+' input.text:eq('+i+')').val($.trim($(this).parent().text()));
				}
			});	
 
			// Abort submit if any mandatory text inputs are empty
			if(failedMandatory == 1) {
				alert (msg);
				return false;	
			}
			else {
				return true;	
			}
		});
	});
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 4 years 5 months ago by tpartner.
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 4 years 5 months ago #70828

  • uxexp
  • uxexp's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
SIMPLY WONDERFUL!!

On a different note: would recommend a book on JavaScript for beginners?
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 4 years 5 months ago #70833

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5899
  • Thank you received: 1291
  • Karma: 520
Hmm...it's been a while...

You'll probably want something on jQuery too - have a look at these:

1) www.peachpit.com/store/product.aspx?isbn=0321772970

2) www.peachpit.com/store/product.aspx?isbn=0321647491
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 4 years 5 months ago #70894

  • uxexp
  • uxexp's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
Thanks much Tony!
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 4 years 3 months ago #74208

  • tomdelf
  • tomdelf's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Hi,

this was exactly what i was looking for, but i'm having problems with the second part, ie. if the checkbox is checked, the comment box is mandatory.

It doesn't work for me and i have no idea why ;) I can just simply submit answers without comment boxes being mandatory, i get no alert messages.

Any clues where to start looking?

Cheers,

Tom
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 4 years 3 months ago #74209

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5964
  • Thank you received: 364
  • Karma: 259
Please post a link to a simple test survey containing the adapted code.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 4 years 3 months ago #74242

  • tomdelf
  • tomdelf's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Sorry, false alarm... seems to work now!

Many thanks in any case for your prompt response!

Tom
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 4 years 3 months ago #74247

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5964
  • Thank you received: 364
  • Karma: 259
tomdelf wrote:
Sorry, false alarm... seems to work now!

Many thanks in any case for your prompt response!

Tom
You're welcome :-)

If our hints have been helpful and you enjoy limesurvey please consider a donation to the team .
We do all this in our free time and you don't have to pay a penny for this software.

Without your help we can't keep this project alive.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 3 years 1 week ago #96163

  • benmok
  • benmok's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 10
  • Karma: 0
Hi - How do you make it the text box only appear for those that the respondent select in the check box?

Thanks,
Ben
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 2 years 3 months ago #105317

  • AGK925
  • AGK925's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 16
  • Thank you received: 2
  • Karma: 0
Hello,

I am new to LimeSurvey and fairly new to Java. I am attempting to use the code above for a survey and I am running into some trouble.

I would like to hide the first three comment boxes and check that the remaining comments are filled in when the corresponding multiple choice is selected. I do not need to pipe in the text responses to another question. Below is the code I am attempting to use to accomplish my tasks.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() { 
 
		var q1ID = 68;
		var msg = 'Please specify in the comment box.';
 
		// Hide the first 3 text inputs of Q1
		$('#question'+q1ID+' li[id^="javatbd"]:eq(0) .comment').hide();
		$('#question'+q1ID+' li[id^="javatbd"]:eq(1) .comment').hide();
		$('#question'+q1ID+' li[id^="javatbd"]:eq(2) .comment').hide();
 
		// Interrupt next/submit function 
		$('#movenextbtn, #movesubmitbtn').click(function(){
 
			// Reset some stuff
			var failedMandatory = 0;
			$('#question'+q1ID+' input.text').css({'background':''});;
 
			// Loop through all check answers in Q1
			$('#question'+q1ID+' input.checkbox:checked').each(function(i){
 
				// Define the row
				var thisRow = $(this).parents('li:eq(0)');
 
				// Check for empty mandatory text inputs
				if($('input.text:visible', thisRow).length > 0 && $('input.text', thisRow).val() == '') {
					$('input.text', thisRow).css({'background':'pink'});
					failedMandatory = 1;
				}
			});	
 
			// Abort submit if any mandatory text inputs are empty
			if(failedMandatory == 1) {
				alert (msg);
				return false;	
			}
			else {
				return true;	
			}
		});
	});
 
</script>

If I just use the first part that hides the text inputs it works fine. However, when I include everything from "// Interrupt next/submit function" down it does not work, and the text inputs are no longer hidden.

Any help would be greatly appreciated.

Tony
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 2 years 3 months ago #105351

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5964
  • Thank you received: 364
  • Karma: 259
Please post a link to an activated test survey.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Hiding multiple choice with comments text box and more 2 years 3 months ago #105363

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5899
  • Thank you received: 1291
  • Karma: 520
Since the inclusion of Expression Manager we need to have a space or line-break after all opening curly braces and before all closing curly braces. (don't get me started on the whole "valid JavaScript" discussion again)

<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() { 
 
		var q1ID = 68;
		var msg = 'Please specify in the comment box.';
 
		// Hide the first 3 text inputs of Q1
		$('#question'+q1ID+' li[id^="javatbd"]:eq(0) .comment').hide();
		$('#question'+q1ID+' li[id^="javatbd"]:eq(1) .comment').hide();
		$('#question'+q1ID+' li[id^="javatbd"]:eq(2) .comment').hide();
 
		// Interrupt next/submit function 
		$('#movenextbtn, #movesubmitbtn').click(function(){
 
			// Reset some stuff
			var failedMandatory = 0;
			$('#question'+q1ID+' input.text').css({ 
				'background':'' 
			});
 
			// Loop through all check answers in Q1
			$('#question'+q1ID+' input.checkbox:checked').each(function(i){
 
				// Define the row
				var thisRow = $(this).parents('li:eq(0)');
 
				// Check for empty mandatory text inputs
				if($('input.text:visible', thisRow).length > 0 && $('input.text', thisRow).val() == '') {
					$('input.text', thisRow).css({
						'background':'pink'
					});
					failedMandatory = 1;
				}
			});	
 
			// Abort submit if any mandatory text inputs are empty
			if(failedMandatory == 1) {
				alert (msg);
				return false;	
			}
			else {
				return true;	
			}
		});
	});
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.
The following user(s) said Thank You: AGK925

Hiding multiple choice with comments text box and more 2 years 3 months ago #105402

  • AGK925
  • AGK925's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 16
  • Thank you received: 2
  • Karma: 0
Thank you very much for your help. It works brilliantly, I spent a lot of time looking for solutions but apparently missed out on the basics of writing the scripts within a question. Now that I have gotten past this hurdle I think it would be wise to spend time learning the basics.

Again thank you very much for your help. This was my first attempt to use LimeSurvey and it seems like it is a great tool for creating online surveys with a wonderfully helpful community.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.800 seconds