Welcome to the LimeSurvey Community Forum

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

Run JavaScript only once

  • Betriebsleiter
  • Betriebsleiter's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 11 months ago #216134 by Betriebsleiter
Run JavaScript only once was created by Betriebsleiter
Hi all, 
one quick question that I could't find a solution to, although I would think it is a common problem.
I added a drop drown to an array questions on page X of my survey using this solution:  LimeSurvey Forum . So far so good.
But I also activated backwards navigation in my survey. Now everytime I go back a page to X-1 and then back again on X it runs the code again, adding new dropdown boxes once again. 
So if I go back twice I get three dropdowns per answer in total: 

Is there any easy to fix to this to run the code only once?

My idea for a workaround would be to add 1 numerical questions (f1) + 1 equation (f2). 
f1 would be a counter. f2 would be {f1 = f1 +1} and the script would then check if f1== 1 and only then run the script. But as soon as I add an equation to the question group, subquestion relevance for other questions stops working... So if there's an easier solution that would be highly appreciated!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 11 months ago #216140 by tpartner
Replied by tpartner on topic Run JavaScript only once
What code did you use?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 11 months ago #216142 by Joffm
Replied by Joffm on topic Run JavaScript only once
Yes, what code did you use?
You see this mentioned thread startd 10 years ago and contains a lot of different things.

With the "actual" code to insert drop-downs in an array(text) I do not see any issue.
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="">...</option>\
  <option value="1">strongly disagree</option>\
  <option value="2">disagree</option>\
  <option value="3">neutral</option>\
  <option value="4">agree</option>\
  <option value="5">strongly agree</option>\
</select>');
 
 
 
    // Listeners
    $('.inserted-select', thisQuestion).on('change', function(i) {
      if($(this).val() != '') {
        $(this).closest('.answer-item').find('input:text').val($('option:selected', this).val()).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());
      $('select.inserted-select', thisCell).val(inputText);
    });
 
    // Clean-up styles
    $('select.inserted-select', thisQuestion).css({
      'max-width': '100%'
    });
    $('.with-select input:text', thisQuestion).css({
      'position': 'absolute',
      'left': '-9999em'
    });
  });
</script>

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Betriebsleiter
  • Betriebsleiter's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 11 months ago #216143 by Betriebsleiter
Replied by Betriebsleiter on topic Run JavaScript only once
I just wanted to share a .lss with the question, but couldn't reproduce this behaviour on my private LS installation. I guess it's an issue somehow caused by the other LS installation..
Code is just the "generic one" from the other thread but yeah, problem seems to be caused by the template or something..
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 wählen</option>\
  <option value="1">Option 1</option>\
  <option value="2">Option 2</option>\
  <option value="3">Option 3</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>

 
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 11 months ago #216193 by tpartner
Replied by tpartner on topic Run JavaScript only once
Depending on the LS version, you may have AJAX enabled in the survey theme options. Always disable that.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Betriebsleiter
  • Betriebsleiter's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 11 months ago #216194 by Betriebsleiter
Replied by Betriebsleiter on topic Run JavaScript only once
That actually solved it, big thanks! Would've never figured that out by myself.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose