- Posts: 9
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Array (text) type question with customized drop down in specific columns
- CultureCommunication
- Topic Author
- Offline
- New Member
Please Log in to join the conversation.
- MC_EMC
- Offline
- New Member
- Posts: 7
- Thank you received: 0
I have been struggling with the following code on limesurvey. I am using javascript for a drop-down array that will ask participants what brand the are using and also provide specific features.
I have trouble with the specific features of the brand. I have already manage to implement the code from tpartner for the use of dropdown menu's in a text array.
However I would like to add 3 columns that would have conditional answers.
For example:
The first dropdown question: Brand A/B/C
Second dropdown question (conditional to which brand has been filled in): Brand A -> Feature 1/2/3; Brand B Feature 4/ 5 / 6; Brand C -> Feature 7-8-9
Third dropdown question (also conditional to the first dropdown question): Brand A -> Feature A1/A2/A3 Brand B -> Feature B1/B2/B3 Brand C -> Feature C1/C2/C3
The code that I have now:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
var qID = {9997};
// Define the select element (dropdown)
var select1 = '<select class="insertedSelect">\
<option value="">-- Please Choose --</option>\
<option value="A">Brand A</option>\
<option value="B">Brand B</option>\
<option value="C">Brand C</option>\
</select>';
var select2 = '<select class="insertedSelect2">\
<option value="">-- Please Choose --</option>\
<option value="AF">Brand A feature 1</option>\
<option value="AF">Brand A feature 2</option>\
<option value="AF">Brand A feature 3</option>\
</select>';
var select3 = '<select class="insertedSelect3">\
<option value="">-- Please Choose --</option>\
<option value="BF">Brand B feature 1</option>\
<option value="BF">Brand B feature 2</option>\
<option value="BF">Brand B feature 3</option>\
</select>';
var select4 = '<select class="insertedSelect4">\
<option value="">-- Please Choose --</option>\
<option value="CF">Brand C feature 1</option>\
<option value="CF">Brand C feature 2</option>\
<option value="CF">Brand C feature 3</option>\
</select>';
// Hide the text inputs
$('#question'+qID+' .answer_cell_00SQ002 input[type="text"]').hide();
$('#question'+qID+' .answer_cell_00SQ003 input[type="text"]').hide();
// Insert the select elements
$('#question'+qID+' .answer_cell_00SQ002').append(select1);
// Initially select an option if the question has already been answered
$('#question'+qID+' .answer_cell_00SQ002 input[type="text"]').each(function(i){
if($(this).val()) {
$(this).closest('td').find('.insertedSelect').val($(this).val());
}
});
// Listener on the dropdowns - insert selected values into hidden text input
$('.insertedSelect').change(function() {
$(this).closest('td').find('input[type="text"]').val($(this).val());
});
/// If statement for selection of dropdown answers based if brand A is selected
$('#question'+qID+' .answer_cell_00SQ002 input[type="text"]').each(function(i){
if($(this).val()!="A") {
// Insert the select elements
$('#question'+qID+' .answer_cell_00SQ003').append(select2);
$('#question'+qID+' .answer_cell_00SQ003 input[type="text"]').each(function(i){
if($(this).val()) {
$(this).closest('td').find('.insertedSelect2').val($(this).val());}
});
// Listener on the dropdowns - insert selected values into hidden text input
$('.insertedSelect2').change(function() {
$(this).closest('td').find('input[type="text"]').val($(this).val());
});
}});
/// If statement for selection of dropdown answers based if brand B is selected
$('#question'+qID+' .answer_cell_00SQ002 input[type="text"]').each(function(i){
if($(this).val()!="B") {
// Insert the select elements
$('#question'+qID+' .answer_cell_00SQ003').append(select3);
$('#question'+qID+' .answer_cell_00SQ003 input[type="text"]').each(function(i){
if($(this).val()) {
$(this).closest('td').find('.insertedSelect3').val($(this).val());}
});
// Listener on the dropdowns - insert selected values into hidden text input
$('.insertedSelect3').change(function() {
$(this).closest('td').find('input[type="text"]').val($(this).val());
});
}});
/// If statement for selection of dropdown answers based if brand C is selected
$('#question'+qID+' .answer_cell_00SQ002 input[type="text"]').each(function(i){
if($(this).val()!="C") {
// Insert the select elements
$('#question'+qID+' .answer_cell_00SQ003').append(select4);
$('#question'+qID+' .answer_cell_00SQ003 input[type="text"]').each(function(i){
if($(this).val()) {
$(this).closest('td').find('.insertedSelect4').val($(this).val());}
});
// Listener on the dropdowns - insert selected values into hidden text input
$('.insertedSelect4').change(function() {
$(this).closest('td').find('input[type="text"]').val($(this).val());
});
}});
});
</script>
I believe there is something wrong with the IF statement that tries to match the answers of the first dropdown question.
There may be a better way to approach this problem, but I have not been able to figure it out yet.
I am open for any suggestions/feedback for this code or on a different approach to this problem.
Please Log in to join the conversation.
- tpartner
- Offline
- LimeSurvey Community Team
- Posts: 10134
- Thank you received: 3603
Please never attach .lsq or .lsg files - they are too time consuming for the volunteers here to handle. Always provide .lss files containing only the relevant questions.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Please Log in to join the conversation.
- Joffm
- Offline
- LimeSurvey Community Team
- Posts: 13096
- Thank you received: 4030
Please, neither provide lsq nor lsg exports.
Reasons:
These both exports are language sensitive. Meaning, you can't import them into a survey with a different base language.
So we have to guess the base language of your export, create a survey, create a group, finally to be able to import your export.
Furthermore these exports do not include the surveywide settings, which may be very important, too.
Help us to help you.
Copy your survey, remove everything not relevant to your question, and send the remaing survey as lss export.
And of course you should tell us the version of LimeSurvey and the theme you are using.
Having a look at your screenshot it seems to be either an old version or a heavy customized theme.
Next question: Where do you find this script?
Reminds a bit of the script to insert dropdowns in "multiple short text" questions.
Joffm
P.S. in one of the former post in this thread you read
You do not substitute QID. {QID} is a placeholder that is filled automatically with the actual question ID
You leave it as it is.
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Please Log in to join the conversation.
- MC_EMC
- Offline
- New Member
- Posts: 7
- Thank you received: 0
Unfortunately, I do not have the possibility to export the questions in a .lss file.
It may be a dutch version, could that help you in importing the lsg file?
I can only provide you with an endnote file of the Javascript code (txt file), because my export possiblities are only in .lsq files.
This text file en .lsg file contain the original code that I derived from an earlier topic about dropdown quesitons in an array. I am sorry that i can not provide you with the lss file.
I found the code at: Array question with answers in dropdown boxes - LimeSurvey Forums - Results from #15
Would this help?
Please Log in to join the conversation.
- holch
- Offline
- LimeSurvey Community Team
- Posts: 11720
- Thank you received: 2756
Therefore you copy your survey, delete all unnecessary clutter and export the Survey as LSS. You probably have to go to the survey level to export the LSS file, not on question level.
The limesurvey version 2.06lts Build 160524
I already expected something ridiculous looking at the screenshot.
Your version was first released on 24/05/2016. Two thousand sixteen! Your installation is over 7 years old and hasn't received any updates, bug fixes or security patches since then.
And it is about 4-5 major versions behind. The support for the version you are using has been stopped for many years now. You really, really, really, really should update your installation to something recent.
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Please Log in to join the conversation.
- MC_EMC
- Offline
- New Member
- Posts: 7
- Thank you received: 0
Therefore I will be limited to this version, hopefully in the future the IT department will update their version, but for now I will have to work with this.
I also did not realize that the lss file is for exporting the complete survey, thank you.
I have attached the lss file.
Please Log in to join the conversation.
- holch
- Offline
- LimeSurvey Community Team
- Posts: 11720
- Thank you received: 2756
I realize that, however I do not have the ability to update our version as this is owned by the Hospital that I work at.
Therefore I will be limited to this version, hopefully in the future the IT department will update their version, but for now I will have to work with this.
things are getting worse. A hospital, potentially collecting patient data, which are deemed to be specially "private" data.
I understand that you might not be in the position to change this on your own, but you should raise your concerns with the IT department and even better with the hospital administration. At least then your conscious is clear. Ideally do it in written and documented form, so that if some data breach happens, you can push all responsibility away from yourself.
If they then do not act upon this (I mean software that is 7 years without an update should ring all alarm bells at the IT department AND the management). Especially, if you include the words GDPR. I am sure no judge would consider LS 2.06 a "state of the art software".
Another issue is, that usually no one has such an old version to try and test things. At least I don't, maybe Joffm and Tpartner do. I run LS3, LS5 and LS6 instances to be able to help in the forum. This are all versions that are still supported at the moment.
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Please Log in to join the conversation.
- tpartner
- Offline
- LimeSurvey Community Team
- Posts: 10134
- Thank you received: 3603
This script, inserted in the question source of an array-texts question, will insert "filter" dropdowns in the second column and dynamically insert dropdowns in the third and fourth columns depending on the answers to those filter dropdowns.
The script is heavily commented so should be easy to follow and should work in LS versions 2-6.
When defining the dropdowns in the "selects" object, the keys (A-3, B-3...) must be combinations of the filter question values and the column positions. For example, a dropdown with key "C-4" will be inserted in column 4 when the filter question is answered as C.
<script type="text/javascript" data-author="Tony Partner"> $(document).ready(function() { var thisQuestion = $('#question{QID}'); if($('.inserted-select', thisQuestion).length == 0) { // Define the dropdowns // Note: the dynamic dropdown keys should be a combination // of the filter question values and the column positions var selects = { 'filter': '<select class="inserted-select form-control">\ <option value="">-- Please Choose --</option>\ <option value="A">Brand A</option>\ <option value="B">Brand B</option>\ <option value="C">Brand C</option>\ </select>', 'A-3': '<select class="inserted-select form-control">\ <option value="">-- Please Choose --</option>\ <option value="A-3-1">Brand A feature 1</option>\ <option value="A-3-2">Brand A feature 2</option>\ <option value="A-3-3">Brand A feature 3</option>\ </select>', 'B-3': '<select class="inserted-select form-control">\ <option value="">-- Please Choose --</option>\ <option value="B-3-1">Brand B feature 1</option>\ <option value="B-3-2">Brand B feature 2</option>\ <option value="B-3-3">Brand B feature 3</option>\ </select>', 'C-3': '<select class="inserted-select form-control">\ <option value="">-- Please Choose --</option>\ <option value="C-3-1">Brand C feature 1</option>\ <option value="C-3-2">Brand C feature 2</option>\ <option value="C-3-3">Brand C feature 3</option>\ </select>', 'A-4': '<select class="inserted-select form-control">\ <option value="">-- Please Choose --</option>\ <option value="A-4-1">Brand A feature 2-1</option>\ <option value="A-4-2">Brand A feature 2-2</option>\ <option value="A-4-3">Brand A feature 2-3</option>\ </select>', 'B-4': '<select class="inserted-select form-control">\ <option value="">-- Please Choose --</option>\ <option value="B-4-1">Brand B feature 2-1</option>\ <option value="B-4-2">Brand B feature 2-2</option>\ <option value="B-4-3">Brand B feature 2-3</option>\ </select>', 'C-4': '<select class="inserted-select form-control">\ <option value="">-- Please Choose --</option>\ <option value="C-4-1">Brand C feature 2-1</option>\ <option value="C-4-2">Brand C feature 2-2</option>\ <option value="C-4-3">Brand C feature 2-3</option>\ </select>' } // Some column-specific classes and attributes $('tr.subquestion-list', thisQuestion).each(function(i) { $('th, td', this).each(function(i) { $(this).addClass('column-'+i); $(this).attr('data-column', i); }); }); // Add some classes to the dynamic array cells $('td.column-2', thisQuestion).removeClass('text-item').addClass('filter-cell with-select dropdown-item'); $('td.column-3', thisQuestion).removeClass('text-item').addClass('dynamic-cell with-select dropdown-item'); $('td.column-4', thisQuestion).removeClass('text-item').addClass('dynamic-cell with-select dropdown-item'); // Hide the text inputs (this could be done with CSS) $('.with-select :text', thisQuestion).hide(); // Insert the filtering dropdown $('.filter-cell', thisQuestion).addClass('with-select').append(selects['filter']); // Listeners on the dropdowns $('.answer-item.with-select', thisQuestion).on('change', '.inserted-select', function(i) { var thisCell = $(this).closest('.answer-item'); // Set the value of the hidden text input var newValue = ''; if($(this).val() != '') { newValue = $(this).val(); } $('input:text', thisCell).val(newValue).trigger('change'); // Specific to the filter dropdown if($(this).closest('.answer-item').hasClass('filter-cell')) { // Handle the dynamic dropdowns handleChildSelects($(this), true); } }); // A function to handle the dynamic dropdowns function handleChildSelects(el, resetCell) { var thisRow = $(el).closest('tr'); var filterValue = $(el).val(); // Loop through the dynamic cells in this row $('.dynamic-cell', thisRow).each(function(i) { // Define the key for the dynamic dropdown var selectKey = filterValue+'-'+$(this).attr('data-column'); // Reset this cell if the fiter question has changed if(resetCell) { $(':text', this).val('').trigger('change'); $('.inserted-select', this).remove(); } // Insert the dropdown $(this).append(selects[selectKey]); // Set the value of the dropdown if($(':text:eq(0)', this).val() != '') { $('.inserted-select', this).val($(':text:eq(0)', this).val()); } }); } // Returning to the page $('.filter-cell', thisQuestion).each(function(i) { if($(':text:eq(0)', this).val() != '') { // Set the value of the filter dropdown $('.inserted-select', this).val($(':text:eq(0)', this).val()); // Handle the dynamic dropdowns handleChildSelects($('.inserted-select', this), false); } }); } }); </script>
Sample 2.06 survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Please Log in to join the conversation.
- MC_EMC
- Offline
- New Member
- Posts: 7
- Thank you received: 0
However, I still agree that the software is limited to the various updates that will provide additional features including safety measurements.
Thank you for highlighting this.
Please Log in to join the conversation.
- MC_EMC
- Offline
- New Member
- Posts: 7
- Thank you received: 0
I have tested and modified the code and it works in the 2.6 version of Limesurvey.
Again my sincere apologies for sharing the lsg file instead of the lss file.
Please Log in to join the conversation.
- MC_EMC
- Offline
- New Member
- Posts: 7
- Thank you received: 0
I would like to show a textfield (questions g3) to eloborate if "Brand A" is selected (first image, the text field is not showing).
The condition statement works if I apply it to the existent "Open questions". When typing "A", it shows the textfield (second image)
I have tried several steps but I can't make limesurvey match the value/text in the textfield. Based on the code, I expected the "Brand A" or value "A" would be inserted in the hidden textfields. Is it possible to set a condition on the hidden textfields?
The condition statement that I am using is: (( Q1_SQ001_SQ002.NAOK == "A")) or (( Q1_SQ001_SQ002.NAOK == "Brand A")) or (( Q1_SQ001_SQ001.NAOK == "A"))
Please Log in to join the conversation.