Welcome to the LimeSurvey Community Forum

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

Populate answers(new answer-item in answers-list) with input from array elements

  • fenryl179
  • fenryl179's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 3 months ago - 2 years 3 months ago #240027 by fenryl179
Please help us help you and fill where relevant:
Your LimeSurvey version: 5.6.2+230125
Own server or LimeSurvey hosting: own
Survey theme/template: fruity
==================

I want to populate the answers-list with elements from either an array or - even better - a csv file. The problem is, that I am an absolute beginner in JS and do not understand the structure of LS fully. What I tried is the following and probably full of errors. The namesArr usually consists of ~300 elements.

Code:
<script type="text/javascript" charset="utf-8">
 
$(document).ready(function() {
var thisQuestion = '#question{QID}';
var namesArr = ['name1',
                'name2',
                'name3',
                'name4'];
//approach1
for (i=0, len=namesArr.length; i<len; i++){
     $('td.answers-list', thisQuestion).push(namesArr)
};
 
//approach2
$('.answer-item'. thisQuestion).each(function(i) {
       $(this).append(namesArr)
});
 
//approach3
$('tr.answers-list', thisQuestion).each(function(i) {
       $(this).append('<td class="answer-item text-item">namesArr
</td>\');
});
 
alert("this: " +thisQuestion+ $('#question{QID} input:text').val());
)};
</script>


So is this even possible to write new answer options in the answers-list?
Last edit: 2 years 3 months ago by fenryl179.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #240028 by tpartner
It looks like you are trying to modify a list-radio or multiple-choice question. Answers/sub-questions for those must be defined in the LimeSurvey backend.

Consider using a text question with autocomplete (search the forum for that).

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.

  • fenryl179
  • fenryl179's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 3 months ago #240029 by fenryl179
The question type is a dropdown list. Yes, I know about the autocomplete possibilty, but to populate answers directly would be nice as well (in combination with searchable_dropdown). With it it is mandatory to pick one of the list. With autocomplete individual entries are possible. I want to avoid that.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago - 2 years 3 months ago #240033 by tpartner
Well, as stated, you cannot dynamically create answers for that question type - LimeSurvey will throw an error.

Regarding forcing a selection from the autocomplete list, you can use the select and change events to test for a list element (ui.item) - stackoverflow.com/questions/18720359/jqu...omplete-force-choice

Alternatively:

1) Use a short-text question.

2) Insert a dynamic searchable select (dropdown) element via the select2 array (or AJAX) data source - select2.org/data-sources/arrays .

3) Place a listener on the select element, using the change event, to load the value into the text input - select2.org/programmatic-control/events .

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 2 years 3 months ago by tpartner.
The following user(s) said Thank You: fenryl179

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose