Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey Cloud, Version 5.6.31
Own server or LimeSurvey hosting: LimeSurvey-Hosting
Survey theme/template: Bootswatch
==================
Hello everybody,
I have a seemingly simple problem regarding a conditional dropdown list inside an array question:
The relevant part consists (or should consist) of 2 question:
The first question is a multiple choice question, where the user has to select all modes of transport (out of a list of 12) he used for a specific journey (for example bike and train).
In the second question he has to give a detailed breakdown of the segments in his journey, for each sement he has to choose the mode of transport and the traveled distance.This should be an array question where the first column is a
dropdown menu with the modes of transport he selected in question 1 and the second column a text input for the distance.
We have absoultely no clue how to implement this type of conditional/dynamic dropdown menu inside the array. I've found somewhat similar problems with dynamic dropdown memenus based on other dropdown menus, but as our dropdown list is created INSIDE an array, it seems a bit more complicated.
Below is our current code for question 2, where we got rid of question 1 and made a dropdown list with ALL modes of transport. This is not our prefered way of doing this tho.
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
var thisQuestion = $('#question{QID}');
// Insert selects
$('.answer-item.answer_cell_X1', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
<option value="">Bitte auswählen, falls zutreffend...</option>\
<option value="1">zu Fuß</option>\
<option value="2">Fahrrad</option>\
<option value="3">Pkw (Benzin/Diesel) als Fahrer*in</option>\
<option value="4">Pkw mit alternativem Antrieb als Fahrer*in (z. B. Elektro, Wasserstoff)</option>\
<option value="5">Pkw (Benzin/Diesel) als Mitfahrer*in</option>\
<option value="6">Pkw mit anderem alternativem Antrieb als Mitfahrer*in (z.B. Elektro, Wasserstoff)</option>\
<option value="7">Bus</option>\
<option value="8">Straßenbahn, U-Bahn</option>\
<option value="9">Bahn (inkl. Schnell-/ Lokalbahn)</option>\
<option value="10">Taxi</option>\
<option value="11">Sonstiges (z.B. E-Scooter, (E-)Motorrad)</option>\
<option value="12">Elektro-Fahrrad</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
$('.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);
});
// Clean-up styles
$('select.inserted-select', thisQuestion).css({
'max-width': '100%'
});
$('.with-select input:text', thisQuestion).css({
'position': 'absolute',
'left': '-9999em'
});
});
</script>
<p>Hier steht ein Erklärungstext</p>
Also the survey example:
Thanks in advance!