Welcome to the LimeSurvey Community Forum

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

Dropdown with ExpressionScript is not evaluated as answer

  • EmanueleZ
  • EmanueleZ's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 week 4 hours ago #268546 by EmanueleZ
Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey Community Edition Version 6.10.0+250106 
Own server or LimeSurvey hosting: Own server
Survey theme/template: Fruity
==================
Hi all,

An option (of a Dropdown menu) have an ExpressionScript for dynamically print the current year:
  • see option.PNG
  • e.g. Test {date("Y")}

The ExpressionScript is correctly show on front-end survey:
  • see survey.PNG
  • e.g. Test 2025

But when the answer is collected, the plain text is show and the ExpressionScript is not evaluated:
  • see answer.PNG
  • e.g. Test {date("Y")} [0]

Is there a way the answer is recorded as show on the front-end (e.g. Test 2025 [0])?

Thanks for the help
 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 10 minutes ago - 1 week 8 minutes ago #268553 by Joffm
Hi,
this is because it is a coded (closed) question.
Meaning, this answer option is stored in the database as is when you activate the survey.
 
But there are other options.
The text is really stored in a questuion of type "array(text)" with this script
array(text) is an open question.
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, select--</option>\
                        <option value="1">Option 1</option>\
                        <option value="2">Option 2</option>\
                        <option value="3">Test {date("Y")}</option>\
                        <option value="4">Option 4</option>\
    </select>');
    
   // Listeners on select elements
    $('.inserted-select', thisQuestion).on('change', function(i) {
      if($(this).val() != '') {
        $(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('keyup');
      }
      else {
        $(this).closest('.answer-item').find('input:text').val('').trigger('keyup');
      }
    });
  // 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>

You see the x-axis subquestion is coded "X001"
 


You may use a question of type "multiple short text" with only one subquestion.
With this script:
Code:
<script type="text/javascript" charset="utf-8">
    $(document).on('ready pjax:complete',function()    {
        var qID = {QID};        
        var inputNum = 1;
 
        // Define the select element (dropdown)
        var prov1 = '<select id="prov1" class="form-control">\
                        <option value="">--Please, select--</option>\
                        <option value="1">Option 1</option>\
                        <option value="2">Option 2</option>\
                        <option value="3">Test {date("Y")}</option>\
                        <option value="4">Option 4</option>\
                    </select>';
         // Hide the text input
        $('#question'+qID+' .question-item:eq('+(inputNum-1)+') input[type="text"]').hide();
 
        // Insert the select elements
        if($('#question'+qID+' .question-item:eq('+(inputNum-1)+') select').length == 0) {
            $('#question'+qID+' .question-item:eq('+(inputNum-1)+') input[type="text"]').before(prov1);
        }        
         
        // Initially select an option if the question has already been answered
        $('#question'+qID+' select').each(function(i) {
            if($.trim($(this).next('input[type="text"]').val()) != '') {
                $(this).val($.trim($(this).next('input[type="text"]').val()));
            }
        });
 
        // Listener on the dropdowns - insert selected values into hidden text input
        $('#question'+qID+' select').change(function() {
            var thisInput = $(this).next('input[type="text"]');
            $(thisInput).val($(this).val());
            checkconditions($(thisInput).attr('value'), $(thisInput).attr('name'), 'text');
        });
 
        // Some styles
        $('#question'+qID+' select').css({
        'margin':'0.3em 0 0 0'
        });
    });
</script>
 

Unfortunately this script only stores the code, not the text.
Maybe somebody more able than I can adapt that the text is stored.


Here a comparison of these three options in the answer table
 

Joffm

On the other side: What for do you want to do this?
Maybe we find a different approach.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 week 8 minutes ago by Joffm.
The following user(s) said Thank You: DenisChenu, EmanueleZ

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 days 7 hours ago #268565 by Joffm
And another - easy - approach.
In a hidden question of type "equation" (d1) set the text, like
{'Test '+date('Y')}

Then pipe this into the answer options of the drop-down question (Q1)
 
At the end of the survey you may remove the value of the equation if the answer was different, like
{d1=if(Q1!=3,'',d1)}

Then you get this in the answer table
 

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

Moderators: holchtpartner

Lime-years ahead

Online-surveys for every purse and purpose