- Posts: 3
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Multiple choice scale instead of dropdown list in array
- indrek
- Topic Author
- Offline
- New Member
Less
More
1 year 3 months ago #239331
by indrek
Multiple choice scale instead of dropdown list in array was created by indrek
Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey Community Edition 5.4.2
Own server or LimeSurvey hosting: own server
Survey theme/template: university's custom theme
==================
There are forum examples of how to change text fields to dropdown lists or to radio buttons in an array. Is it possible to change such an array so that one of the dropdown lists is presented as multiple choices? What I'm looking for is a way to present several mutually non-exclusive answer options per cell in an array. A dropdown list or radio buttons would work for mutually exclusive answer options, but that's not what I'm looking for here.
Your LimeSurvey version: LimeSurvey Community Edition 5.4.2
Own server or LimeSurvey hosting: own server
Survey theme/template: university's custom theme
==================
There are forum examples of how to change text fields to dropdown lists or to radio buttons in an array. Is it possible to change such an array so that one of the dropdown lists is presented as multiple choices? What I'm looking for is a way to present several mutually non-exclusive answer options per cell in an array. A dropdown list or radio buttons would work for mutually exclusive answer options, but that's not what I'm looking for here.
Please Log in to join the conversation.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10099
- Thank you received: 3589
1 year 3 months ago #239339
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Multiple choice scale instead of dropdown list in array
I would prefer to use check-boxes as respondents are more familiar with them and select elements with the "multiple" attribute are displayed expanded by most browsers anyway.
Having said that, this script in the source of an array-texts will insert drop-downs with the "multiple" attribute into the first column.
Sample survey attached:
Having said that, this script in the source of an array-texts will insert drop-downs with the "multiple" attribute into the first column.
Code:
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ var thisQuestion = $('#question{QID}'); if($('.inserted-select', thisQuestion).length == 0) { // 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 $('.answer-item.column-1', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select" multiple>\ <option value="" selected>Please choose...</option>\ <option value="option1">Option 1</option>\ <option value="option2">Option 2</option>\ <option value="option3">Option 3</option>\ <option value="option4">Option 4</option>\ </select>'); // Listeners $('.inserted-select', thisQuestion).on('change', function(i) { if($(this).val() != '') { console.log($(this).val()); $(this).closest('.answer-item').find('input:text').val($(this).val()).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()); if(inputText != '') { $('select.inserted-select option', thisCell).removeAttr('selected'); var inputValues = inputText.split(','); $.each(inputValues, function(i, val) { $('select.inserted-select option[value="'+val+'"]', thisCell).attr('selected', true); }); } }); // 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:
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: indrek
Please Log in to join the conversation.
- indrek
- Topic Author
- Offline
- New Member
Less
More
- Posts: 3
- Thank you received: 0
1 year 3 months ago #239350
by indrek
Replied by indrek on topic Multiple choice scale instead of dropdown list in array
Thanks a lot, Tony, that's pretty much what I was trying to achieve! However, check boxes would indeed be more intuitive for the respondent as they can be used with mouse only. Is there a way to present the same answer options as check boxes?
Please Log in to join the conversation.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10099
- Thank you received: 3589
1 year 3 months ago #239351
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Multiple choice scale instead of dropdown list in array
This script will insert check-boxes into column-1 of an array question:
Sample survey attached:
Code:
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ var thisQuestion = $('#question{QID}'); if($('.inserted-select', thisQuestion).length == 0) { // Column-specific classes $('tr.subquestion-list', thisQuestion).each(function(i) { $('th, td', this).each(function(i) { $(this).addClass('column-'+i); }); }); // Insert checkboxes into column 1 $('.answer-item.column-1', thisQuestion).each(function(i) { var column = 1; var row = i+1; $(this).addClass('with-multi-opt').append('<ul class="list-unstyled inserted-list">\ <li class="question-item checkbox-item inserted-item">\ <input type="checkbox" id="'+row+'_'+column+'_box1" value="option1">\ <label for="'+row+'_'+column+'_box1" class="checkbox-label control-label">Option 1</label>\ </li>\ <li class="question-item checkbox-item inserted-item">\ <input type="checkbox" id="'+row+'_'+column+'_box2" value="option2">\ <label for="'+row+'_'+column+'_box2" class="checkbox-label control-label">Option 2</label>\ </li>\ <li class="question-item checkbox-item inserted-item">\ <input type="checkbox" id="'+row+'_'+column+'_box3" value="option3">\ <label for="'+row+'_'+column+'_box3" class="checkbox-label control-label">Option 3</label>\ </li>\ </ul>'); }); // Listeners $('.inserted-item :checkbox', thisQuestion).on('change', function(i) { var thisCell = $(this).closest('.answer-item'); var values = []; $('.inserted-item :checkbox:checked', thisCell).each(function(i) { values.push($(this).val()); }); $('input:text', thisCell).val(values); }); // Returning to page $('.with-multi-opt input:text', thisQuestion).each(function(i) { var thisCell = $(this).closest('.answer-item'); var inputValues = $.trim($(this).val()); if(inputValues != '') { console.log(inputValues); var prevValues = inputValues.split(','); $.each(prevValues, function(i, val) { $('.inserted-item :checkbox[value="'+val+'"]', thisCell).prop('checked', true); }); } }); // Clean-up styles $('.inserted-list', thisQuestion).css({ 'margin': '0', }); $('.inserted-list li', thisQuestion).css({ 'margin': '5px 0 -5px 0', 'padding': '0 0 0 20px', 'text-align': 'left' }); $('.with-multi-opt input:text', thisQuestion).css({ 'position': 'absolute', 'left': '-9999em' }); } }); </script>
Sample survey attached:
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: indrek
Please Log in to join the conversation.
- indrek
- Topic Author
- Offline
- New Member
Less
More
- Posts: 3
- Thank you received: 0
1 year 3 months ago #239371
by indrek
Replied by indrek on topic Multiple choice scale instead of dropdown list in array
Awesome, many thanks!
Please Log in to join the conversation.