Welcome to the LimeSurvey Community Forum

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

Customizing List (Dropdown) Question Type

  • ititca
  • ititca's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 6 months ago #174577 by ititca
Replied by ititca on topic Customizing List (Dropdown) Question Type
3.14.8+180829

I believe the most recent stable version.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 6 months ago #174586 by tpartner
Replied by tpartner on topic Customizing List (Dropdown) Question Type
I'm not sure why you are seeing errors, I saw none when importing your survey.

This will insert drop-downs into column 1 and numeric inputs with 0-60 allowed in column 2.

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Column-specific classes
    $('tr.subquestion-list', thisQuestion).each(function(i) {
      $('th, td', this).each(function(i) {
        $(this).addClass('column-'+i);
      });
    });
 
    // Insert selects into column 1
    if($('.answer-item.column-1 .inserted-select', thisQuestion).length == 0) {
      $('.answer-item.column-1', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
        <option value="">Please choose...</option>\
        <option value="1">Yes</option>\
        <option value="2">No</option>\
        <option value="3">Do not know</option>\
      </select>');
    } 
 
    // Listeners on select elements
    $('.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
    $('.with-select 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);
    });
 
    // Listener on column 2 inputs
    $('.answer-item.column-2 input:text', thisQuestion).on('keyup change', function(e) {
      var thisValue = $.trim($(this).val());
 
      // Numerics only
      if($.isNumeric(thisValue) === false) {
 
        // Strip out non-numerics characters
        newValue = thisValue.replace(/\D/g,'');
        $(this).val(newValue).trigger('change');
      }
 
      // Max/min values
      var maxAllowed = 60;
      var minAllowed = 0;
      if (thisValue > maxAllowed) {
        alert('The maximum allowed is '+maxAllowed+'.');
        $(this).val(maxAllowed);
      }
      else if (thisValue < minAllowed) {
        alert('The minimum allowed is '+minAllowed+'.');
        $(this).val(minAllowed);
      }       
    });
 
    // Clean-up styles
    $('select.inserted-select', thisQuestion).css({
      'max-width': '100%'
    });
    $('.with-select input:text', thisQuestion).css({
      'position': 'absolute',
      'left': '-9999em'
    });
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...6657.lss
File Size:27 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: ititca
The topic has been locked.
  • ititca
  • ititca's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 6 months ago #174659 by ititca
Replied by ititca on topic Customizing List (Dropdown) Question Type
Thank you for your assistance Tony. Greatly appreciated. Two additional questions:

If I wanted to set a condition that the same option in the dropdown list can't be selected more than once in a question, how would I make this happen?

Lastly... What's the best way for me to send you a thank-you/funds to have a beer or two on me? :)
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 6 months ago #174669 by tpartner
Replied by tpartner on topic Customizing List (Dropdown) Question Type
Your welcome.

1) You should be able to do that with a question validation equation something like this:

Code:
{unique(that.Q1.sq_X001)}

Sample survey attached:

File Attachment:

File Name: limesurvey...7116.lss
File Size:27 KB



2) Thanks for the offer but no beers required. :)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: ititca
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose