Welcome to the LimeSurvey Community Forum

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

Array(Text) default answers in LS 3.X

  • frederikbrandenstein
  • frederikbrandenstein's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 9 months ago #186694 by frederikbrandenstein
Array(Text) default answers in LS 3.X was created by frederikbrandenstein
I use the code as outlined in this answer for masking values with text in dropdown menus.

I would additionally like the answers to default to some string "2".

For LS 2.X there are some solutions given in the forum, like here .

I struggle to adapt it to my LS 3.16 installation.

I guess there is sth. that should do what I mean in the section ///Default to "2" in the following code. While it's not working, you most probably get the line of thought:
Code:
$(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
     /// Default to "2"  ----- doesn't work :P 
    $('.inserted-select', thisQuestion).each(function(i){
  $(this).closest('.answer-item').prop('value', '2')
    });
 
 
    // Insert selects
    $('.answer-item', thisQuestion).append('<select class="inserted-select form-control list-question-select">\
                          <option value="1">+</option>\
                          <option value="2">0</option>\
                          <option value="3">-</option>\
        <option value="">Keine Angabe</option>\
                        </select>');  
 
    // Listeners
    $('.inserted-select', thisQuestion).on('change', function(i) {
      if($(this).val() != '') {
        $(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
      }
      else {
        $(this).closest('.answer-item').find('input:text').val('').trigger('change');
      }
    });
 
    // Returning to page
    $('input:text', thisQuestion).each(function(i) {
      var thisCell = $(this).closest('.answer-item');
      var inputText = $.trim($(this).val());
      var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
      $('select.inserted-select', thisCell).val(selectval);
    });
 
    // Clean-up styles
    $('select.inserted-select', thisQuestion).css({
      'max-width': '100%'
    });
    $('input:text', thisQuestion).css({
      'position': 'absolute',
      'left': '-9999em'
    });
  });
 
 

As always, I'd appreciate any help .
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 9 months ago #186729 by tpartner
Replied by tpartner on topic Array(Text) default answers in LS 3.X
Can you attach a small sample survey (.lss file) containing only the relevant question and your JavaScript?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • frederikbrandenstein
  • frederikbrandenstein's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 8 months ago - 4 years 8 months ago #186743 by frederikbrandenstein
Replied by frederikbrandenstein on topic Array(Text) default answers in LS 3.X
Find an example attached. This works; nagging me is that it doesn't default to some value, let's say "cannot decide" in the example case.
Last edit: 4 years 8 months ago by frederikbrandenstein.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 8 months ago #186781 by tpartner
Replied by tpartner on topic Array(Text) default answers in LS 3.X
I think this may do the trick:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item', thisQuestion).append('<select class="inserted-select form-control list-question-select">\
                          <option value="1">+</option>\
                          <option value="2">cannot decide</option>\
                          <option value="3">-</option>\
                          <option value="">None</option>\
                        </select>');  
 
    // Listeners
    $('.inserted-select', thisQuestion).on('change', function(i) {
      if($(this).val() != '') {
        $(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
      }
      else {
        $(this).closest('.answer-item').find('input:text').val('').trigger('change');
      }
    });
 
    // Initial state
    $('input:text', thisQuestion).each(function(i) {
      var thisCell = $(this).closest('.answer-item');
      var inputText = $.trim($(this).val());
      var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
      $('select.inserted-select', thisCell).val(selectval);
 
      // Default answer
      var defaultOption = '2';
      if(inputText == '') {
        $(this).val($('select.inserted-select option[value="'+defaultOption+'"]', thisCell).text());
        $('select.inserted-select', thisCell).val(defaultOption);
      }
    });
 
    // Clean-up styles
    $('select.inserted-select', thisQuestion).css({
      'max-width': '100%'
    });
    $('input:text', thisQuestion).css({
      'position': 'absolute',
      'left': '-9999em'
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 8 months ago #186783 by DenisChenu
Replied by DenisChenu on topic Array(Text) default answers in LS 3.X
@tpartner : if i don't mlake error : user can not answer «none» here : moving previous : none become 2.

No ?

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 8 months ago #186786 by tpartner
Replied by tpartner on topic Array(Text) default answers in LS 3.X
I don't think so. In this case, the (hidden) text inputs are populated with the dropdown option text, not its value.

So, selecting "None" loads the input with "None" so it is not empty on returning to the page.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • frederikbrandenstein
  • frederikbrandenstein's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 8 months ago #186848 by frederikbrandenstein
Replied by frederikbrandenstein on topic Array(Text) default answers in LS 3.X
Thank you very much. It works not only in the MWE above but also in my productive survey.

Regards.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose