Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Hiding multiple choice with comments text box and more

More
12 years 3 months ago - 12 years 3 months ago #70804 by uxexp
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: 12 years 3 months ago by uxexp.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 3 months ago - 12 years 3 months ago #70818 by tpartner
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)
Code:
<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 &amp;&amp; $('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: 12 years 3 months ago by tpartner.
The topic has been locked.
More
12 years 3 months ago #70828 by uxexp
SIMPLY WONDERFUL!!

On a different note: would recommend a book on JavaScript for beginners?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 3 months ago #70833 by tpartner
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 topic has been locked.
More
12 years 3 months ago #70894 by uxexp
Thanks much Tony!
The topic has been locked.
More
12 years 1 month ago #74208 by tomdelf
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 topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
12 years 1 month ago #74209 by Mazi
Please post a link to a simple test survey containing the adapted code.

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
12 years 1 month ago #74242 by tomdelf
Sorry, false alarm... seems to work now!

Many thanks in any case for your prompt response!

Tom
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
12 years 1 month ago #74247 by Mazi

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
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
10 years 10 months ago #96163 by benmok
Hi - How do you make it the text box only appear for those that the respondent select in the check box?

Thanks,
Ben
The topic has been locked.
More
10 years 3 weeks ago #105317 by AGK925
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.
Code:
<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 &amp;&amp; $('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 topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
10 years 3 weeks ago #105351 by Mazi
Please post a link to an activated test survey.

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose