Welcome to the LimeSurvey Community Forum

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

Array question with answers in dropdown list

  • Someone10
  • Someone10's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 9 months ago #169270 by Someone10
Hi, I used this code for a survey

<script type="text/javascript" charset="utf-8">

$(document).ready(function(){

$('#question9769 .answer_cell_00SQ001 option[value=1]').text('No recurro');
$('#question9769 .answer_cell_00SQ001 option[value=2]').text('Arocena Gustavo');
$('#question9769 .answer_cell_00SQ001 option[value=3]').text('Aspra Manuel');
$('#question9769 .answer_cell_00SQ001 option[value=4]').text('Bravo Ricardo');
$('#question9769 .answer_cell_00SQ001 option[value=5]').text('Zalazar Martin');

$('#question9769 .answer_cell_00SQ002 option[value=1]').text('No recurro');
$('#question9769 .answer_cell_00SQ002 option[value=2]').text('Bravo Ricardo');
$('#question9769 .answer_cell_00SQ002 option[value=3]').text('Fernandez Flavio');
$('#question9769 .answer_cell_00SQ002 option[value=4]').text('Zalazar Martin');
$('#question9769 .answer_cell_00SQ002 option[value=5]').text('').hide();

});

</script>

But dont work, i used the Versión 3.8.0+180522.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169278 by tpartner
Replied by tpartner on topic Array question with answers in dropdown list
Can you provide an export of a small sample survey?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Someone10
  • Someone10's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 9 months ago #169294 by Someone10
Replied by Someone10 on topic Array question with answers in dropdown list
Ok
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169319 by tpartner
Replied by tpartner on topic Array question with answers in dropdown list
Try this:

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    $('#question{QID} .answer_cell_SQ001 option[value=1]').text('No recurro');
    $('#question{QID} .answer_cell_SQ001 option[value=2]').text('Arocena Gustavo');
    $('#question{QID} .answer_cell_SQ001 option[value=3]').text('Aspra Manuel');
    $('#question{QID} .answer_cell_SQ001 option[value=4]').text('Bravo Ricardo');
    $('#question{QID} .answer_cell_SQ001 option[value=5]').text('Zalazar Martin');
 
    $('#question{QID} .answer_cell_SQ002 option[value=1]').text('No recurro');
    $('#question{QID} .answer_cell_SQ002 option[value=2]').text('Bravo Ricardo');
    $('#question{QID} .answer_cell_SQ002 option[value=3]').text('Fernandez Flavio');
    $('#question{QID} .answer_cell_SQ002 option[value=4]').text('Zalazar Martin');
    $('#question{QID} .answer_cell_SQ002 option[value=5]').text('').hide();
 
  });
 
</script>

Here is your sample survey with that change:

File Attachment:

File Name: limesurvey...9661.lss
File Size:18 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Someone10
  • Someone10's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 9 months ago #169321 by Someone10
Replied by Someone10 on topic Array question with answers in dropdown list
Thank you very much tony, it works. But another question arises. Is there any way to put in the middle of those columns a column in which you can enter text?

If there was some kind of code that would allow me in one of those columns to add text, I would fix many problems
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169325 by tpartner
Replied by tpartner on topic Array question with answers in dropdown list
For that, you could extend this workaround for separate columns - www.limesurvey.org/forum/can-i-do-this-w...al-text-value#168857

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Someone10
  • Someone10's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 9 months ago #169330 by Someone10
Replied by Someone10 on topic Array question with answers in dropdown list
I have imported the survey from that page with the code and I get the table without a column with text to add, what could have gone wrong?

The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169344 by tpartner
Replied by tpartner on topic Array question with answers in dropdown list
Yes, that's why I said you would have to extend the workaround for different columns.

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_X001', 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>'); 
    $('.answer-item.answer_cell_X002', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Please choose...</option>\
                          <option value="1">Red</option>\
                          <option value="2">Blue</option>\
                          <option value="3">Pink</option>\
                          <option value="3">Purple</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>



Survey attached:

File Attachment:

File Name: limesurvey...1969.lss
File Size:21 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Someone10
  • Someone10's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 9 months ago - 5 years 9 months ago #169365 by Someone10
Replied by Someone10 on topic Array question with answers in dropdown list
thank you very much, one last question, if I want to add more select, how could I do? I want to do as in the image that I attached, that X004, X005 and X007 are dropdown
Last edit: 5 years 9 months ago by Someone10.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169371 by Joffm
Hi,
I am sure you can find out by yourself.

In the javascript code you see
Code:
// Insert selects
    $('.answer-item.answer_cell_X001', 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>');
You see the X001 refers to the column code.

So just define your rows and columns and add the appropriate lines in the script as I did here:
Code:
$('.answer-item.answer_cell_X005', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Please choose...</option>\
                          <option value="1">AAA</option>\
                          <option value="2">BBB</option>\
                          <option value="3">Do not know</option>\
                        </select>'); 
    $('.answer-item.answer_cell_X006', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Please choose...</option>\
                          <option value="1">AAA</option>\
                          <option value="2">BBB</option>\
                          <option value="3">Do not know</option>\
                        </select>'); 




Regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Someone10
  • Someone10's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 9 months ago #169377 by Someone10
Replied by Someone10 on topic Array question with answers in dropdown list
I tried with this code but dont work
Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_X004', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Elige una opción...</option>\
                          <option value="1">a</option>\
                          <option value="2">b</option>\
                        </select>'); 
    $('.answer-item.answer_cell_X005', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Please choose...</option>\
                          <option value="1">a</option>\
                          <option value="2">b</option>\
                          <option value="3">c</option>\
 
                        </select>');
      $('.answer-item.answer_cell_X007', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Please choose...</option>\
                          <option value="1">a</option>\
                          <option value="2">b</option>\
                          <option value="3">c</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>

with this survey
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169382 by tpartner
Replied by tpartner on topic Array question with answers in dropdown list
That is because you have introduced some white-spaces after the back-slashes - these are not allowed.

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_X004', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Elige una opción...</option>\
                          <option value="1">Certificada</option>\
                          <option value="2">No Certificada</option>\
                        </select>');
    $('.answer-item.answer_cell_X005', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Please choose...</option>\
                          <option value="1">Suya</option>\
                          <option value="2">Cooperativa</option>\
                          <option value="3">Otro</option>\
                          <option value="3">Purple</option>\
                        </select>');
    $('.answer-item.answer_cell_X007', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                          <option value="">Please choose...</option>\
                          <option value="1">No hace nada</option>\
                          <option value="2">Limpia Coop</option>\
                          <option value="3">Limpia+Trata</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>

Sample survey attached:

File Attachment:

File Name: limesurvey...1925.lss
File Size:23 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose