Welcome to the LimeSurvey Community Forum

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

How to add a button that changes being mandatory situation of another question?

  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 3 months ago #226468 by LSWipo
Please help us help you and fill where relevant:
Your LimeSurvey version: 3.0 Theme Editor
Own server or LimeSurvey hosting:
Survey theme/template: test_theme
==================
Hello! 

There is a question in the survey where people write their income into the box after selecting the unit (hourly, monthly, yearly) which there get paid in. It is a mandatory question. I would like to add a button next to it saying "I do not know", ideally just next to the unit box. If they click the button, they can leave the unit box and the income box empty. They also should not be allowed to fill the boxes and click the button at the same time. How could I implement this? 

 

Thanks in advance! 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #226471 by Joffm
Hi,
you didn't say which question types you are using.

Here is a solution with an array(text) and drop-down feature.
And furthermore the second column is disabled if the respondent selects "DK".

 

x-axis coded "X001", "X002", ...
Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_X001', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
            <option value="">...</option>\
            <option value="1">hourly</option>\
            <option value="2">monthly</option>\
            <option value="4">yearly</option>\
            <option value="9">I don\'t know</option>\
    </select>');
 
    // Listeners
    $('.inserted-select', thisQuestion).on('change', function(i) {
      if($(this).val() != '') {
        $(this).closest('.answer-item').find('input:text').val($('option:selected', this).val()).trigger('change');
      }
      else {
        $(this).closest('.answer-item').find('input:text').val('').trigger('change');
      }
 
 
      // 3rd column conditional on 2nd column
      if($(this).closest('.answer-item').hasClass('answer_cell_X001')) {
         handleColumnX002($(this));
      }
    });
 
    function handleColumnX002(thisSelect) {
      var thisRow = $(thisSelect).closest('tr.subquestion-list');
      var item3 = $('.answer_cell_X002', thisRow);
      if($(thisSelect).val() < '9') {
        $('input', item3).prop('disabled', false);
              }
      else {
        $('input:text', item3).val('').prop('disabled', true);
        $('input:text', item3).val('').trigger('change');
      }
    }
 
  // Returning to page
    $('.with-select input:text', thisQuestion).each(function(i) {
      var thisCell = $(this).closest('.answer-item');
      var inputText = $.trim($(this).val());
      $('input:text', thisCell).val(inputText);
    });
 
    // Clean-up styles
    $('select.inserted-select', thisQuestion).css({
      'max-width': '100%'
    });
    $('.with-select input:text', thisQuestion).css({
      'position': 'absolute',
      'left': '-9999em'
    });
  });
</script>

Joffm

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: LSWipo

Please Log in to join the conversation.

  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 3 months ago #226483 by LSWipo
Hello,

Thank you for your response.

For the unit question, the type I use is dropdown and for the earnings, it is numerical input. These are not in the same question but in two different questions. Should I put them at first in the same question to use the code you wrote? And to be sure, should I add this code to the source in the question? Please see the lss export for further details.

 

File Attachment:

File Name: limesurvey...8243.lss
File Size:1,091 KB

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago - 3 years 3 months ago #226484 by Joffm
Read my tutorial about arrays
"Tutorial 1: Matrizen"
in the German part.

Joffm

P.S.
Vor einem Monat habe ich schon drei Lösungen gezeigt, dieses merkwürdige "Drei-Fragen-Layout" vermeiden.
Darauf bist Du nicht eingegangen

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 3 months ago by Joffm.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #226496 by Joffm
Here I changed "disabled" to "readonly" and set  a value to this field.
Now it is not necessary to validate.
Just a mandatory question.
Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_X001', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
              <option value="">...</option>\
            <option value="1">Stunde</option>\
            <option value="2">Monat</option>\
            <option value="3">Jahr</option>\
            <option value="9">Weiß nicht</option>\
    </select>');
 
    // Listeners
    $('.inserted-select', thisQuestion).on('change', function(i) {
      if($(this).val() != '') {
        $(this).closest('.answer-item').find('input:text').val($('option:selected', this).val()).trigger('change');
      }
      else {
        $(this).closest('.answer-item').find('input:text').val('').trigger('change');
      }
 
 
      // 2nd column conditional on 1st column
      if($(this).closest('.answer-item').hasClass('answer_cell_X001')) {
         handleColumnX002($(this));
      }
    });
 
    function handleColumnX002(thisSelect) {
      var thisRow = $(thisSelect).closest('tr.subquestion-list');
      var item2 = $('.answer_cell_X002', thisRow);
      if($(thisSelect).val() < '9') {
        $('input:text', item2).prop('readonly', false);
        $('input:text', item2).val('').trigger('change');
      }
      else {
        $('input:text', item2).val('').prop('readonly', true);
        $('input:text', item2).val('- NA -').trigger('change');
      }
    }
 
  // Returning to page
    $('.with-select input:text', thisQuestion).each(function(i) {
      var thisCell = $(this).closest('.answer-item');
      var inputText = $.trim($(this).val());
      $('select.inserted-select', thisCell).val(inputText);
    });
 
    // Clean-up styles
    $('select.inserted-select', thisQuestion).css({
      'max-width': '100%'
    });
    $('.with-select input:text', thisQuestion).css({
      'position': 'absolute',
      'left': '-9999em'
    });
  });
</script>

 

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: LSWipo

Please Log in to join the conversation.

  • LSWipo
  • LSWipo's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 2 months ago #226742 by LSWipo
Thank you for your response.

I tried to change the question type to array but then this affected the calculations at the later stages for the earnings question. Is there a possible solution to this without changing the question type from numerical input? For example, is it possible to add a warning being shown when a respondent clicks the button and fills the boxes at the same time and make the respondent not continue the survey as long as the warning appears?

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago - 3 years 2 months ago #226753 by Joffm

 this affected the calculations at the later stages

That should be the least thing - to change this variable in the calculation or even insert an equation to set a variable of the desired type and name.

is it possible to add a warning being shown when a respondent clicks the button and fills the boxes at the same time

Of course. This is simple validation.
As you still seem to stick to the multi question solution,
one question to show the text and two to enter the data,
you may insert the validation equation in the second question (now you can't proceed)
and you may include your error message in the text question displayed by an IF-statement (here shown as alert)
You may read about alerts here
[url] getbootstrap.com/docs/3.4/javascript/#alerts [/url]

or read my "Tutorial III: Gimmicks" in the German section.

Like this
 



Not the best solution, but possible.


Or this easy solution:
question of type "multiple short text"
first subquestion with inserted dropdown
Code:
<script type="text/javascript" charset="utf-8">
    $(document).on('ready pjax:complete',function()    {
        var qID = {QID};        
        var inputNum = 1;
 
        // Define the select element (dropdown)
        var prov1 = '<select id="prov1" class="form-control">\
                        <option value="">--Bitte, wählen--</option>\
                        <option value="1">Stunde</option>\
                        <option value="2">Monat</option>\
                        <option value="3">Jahr</option>\
                        <option value="4">Keine Ahnung</option>\
                    </select>';
         // Hide the text input
        $('#question'+qID+' .question-item:eq('+(inputNum-1)+') input[type="text"]').hide();
 
        // Insert the select elements
        if($('#question'+qID+' .question-item:eq('+(inputNum-1)+') select').length == 0) {
            $('#question'+qID+' .question-item:eq('+(inputNum-1)+') input[type="text"]').before(prov1);
        }        
         
        // Initially select an option if the question has already been answered
        $('#question'+qID+' select').each(function(i) {
            if($.trim($(this).next('input[type="text"]').val()) != '') {
                $(this).val($.trim($(this).next('input[type="text"]').val()));
            }
        });
 
        // Listener on the dropdowns - insert selected values into hidden text input
        $('#question'+qID+' select').change(function() {
            var thisInput = $(this).next('input[type="text"]');
            $(thisInput).val($(this).val());
            checkconditions($(thisInput).attr('value'), $(thisInput).attr('name'), 'text');
        });
 
        // Some styles
        $('#question'+qID+' select').css({
        'margin':'0.3em 0 0 0'
        });
    });
</script>

second subquestion with
subquestion relevance equation: Q1_SQ001!=4
and array filter type set to "deaktiviert"
   



Joffm

Außerdem habe ich Euch dazu jetzt genug gezeigt.





 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 2 months ago by Joffm.
The following user(s) said Thank You: LSWipo

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose