Welcome to the LimeSurvey Community Forum

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

Extra Javascript not working on IOS devices

  • MaBo97
  • MaBo97's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
8 months 2 weeks ago - 8 months 2 weeks ago #245620 by MaBo97
Please help us help you and fill where relevant:
Your LimeSurvey version: Version 5.6.31
Own server or LimeSurvey hosting: LimeSurvey Cloud
Survey theme/template: own / based on fruity
==================
Hey there,

I'm a a very happy LimeSurvey user and am using it for my company. I have a question regarding javascript. We are offering education and evaluate our courses using limesurvey. The courses are organized in diffent fields which are taught by various teachers and each individual course deals with a specific topic. At the beginning we have a series of dropdown questions that are supposed to be "cascaded". What we wanted to implement, and it's working on android already, is that if you select a field, only the teachers and topics of that field should be displayed in the drop down questions.

As there are changes in our staff and the course topics from time to time we wanted to implement the cascading with javascript. I know it could also be done natively with multiple "paths" through the questionaire, but this would mean to create a LOT of questions each time a change is happening. This is unfeasible using the gui (maybe it could be done quickly by editing the text of the import-export file). Javascript can be changed easily without taking the down the survey and i have already written some helper scripts to make it even easier. However, here's the problem:

The javascript code is working nicely on android devices and computer, but it doesn't work on IOS devices (so far we only tested mobile devices). I have no idea what might be behind this. I thought it's something like javascript disabled by default, but my IOS friends tell me that can you any website without a problem. Do you have any ideas how to fix this?

I'm not a programmer so the problem might be in the code, although I think it's not very complicated. I changed a code snippet by TPartner I found online (therefore the reference). This is what we are using right now (the "code" formatting didn't show a preview, so i used a blockquote instead) inside the "topic" question:

<span style="font-size:18px;">Kursbezeichnung:</span> <script type="text/javascript" data-author="Tony Partner">
    $(document).on('ready pjax:scriptcomplete', function() {
          var thisQuestion = $('#question{QID}');
                var secondContainer = thisQuestion.prevAll('.question-container').eq(1);
        
        // Define the excluded options
var excludes = {
    'A1': , // Bau
    'A2': , // Elektro
    'A3': , // Metall
    'A4': , // Sanitär-, Heizung-, Klimatechnik
    'A5': , // Holz
    'A6': , // Farbe und Gestaltung
    'A7': , // Fahrzeuge
    'A8': , // Friseur und Kosmetik
    'A9': , // Büro und Verwaltung
};

          $('.answer-item select', secondContainer).on('change', function(i) {
            // Reset the dropdown
            $('select.form-control', thisQuestion).val('').trigger('change');
            $('select.form-control option', thisQuestion).show();
            
            // Get the value of the previous question
            var selectedValue = $(this).val();
            
            // Hide answer options based on selectedValue
            $.each(excludes[selectedValue], function(i, val) {
                $('select.form-control option', thisQuestion).filter(function() {
                    return $(this).val() === val;
                }).hide();
            });
        });
    });
</script>


As i said I have another script which helps me generate the contents of "excludes".

Why is this not working on IOS devices?? How can I make work? Is there a maintainable alternative approach?

Thank you very much! Limesurvey is fantastic!

David
Last edit: 8 months 2 weeks ago by MaBo97. Reason: There was a link inserted from copying I didn't want to be there...

Please Log in to join the conversation.

  • MaBo97
  • MaBo97's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
8 months 2 weeks ago - 8 months 2 weeks ago #245621 by MaBo97
Replied by MaBo97 on topic Extra Javascript not working on IOS devices
ok, strangely the code in my blockquote is being displayed in an altered way. The exclude stuff looks like this (example with empty spaces):

' A 1 ' :  [ ' A O 2 8 ' ,  ' A O 2 9 ' ] ,  / /  B a u
' A 2 ' :  [ ' A O 0 1 ' ,  ' A O 0 2 ' ] ,  / /  E l e k t r o

but from my understanding it shouldn't matter anyways...
Last edit: 8 months 2 weeks ago by MaBo97. Reason: what i wanted to show was again cut. now it should be there

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 months 2 weeks ago #245632 by tpartner
Replied by tpartner on topic Extra Javascript not working on IOS devices
Can you give a link to the script that you copied?

Can you attach a small sample survey (.lss file) 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.

  • MaBo97
  • MaBo97's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
8 months 2 weeks ago #245637 by MaBo97
Replied by MaBo97 on topic Extra Javascript not working on IOS devices
Dear Tony,

thank you for your reply! :) Unfortunately I can't give you a link to the code I changed because it's been some time since I initially created it. Actually the final result in the survey might be a mash of multiple code pieces. I've been trying around until it worked. As I said I'm not a programmer and my ability to read code is limited.

I have attached an lss of the minimal survey which only contains the first three questions. There is also code in the second question, which is also not working and it's very close to the code I posted above. I have also checked whether the problem is still present in the minimal example and it is. It's working on Android and Windows / Linux computers.

I hope we can find a solution.

Thank you again!
David

File Attachment:

File Name: limesurvey...imal.lss
File Size:118 KB

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 months 2 weeks ago - 8 months 2 weeks ago #245650 by tpartner
Replied by tpartner on topic Extra Javascript not working on IOS devices
The problem is that some versions of Safari will not allow hiding select options. The irrelevant options must be removed, so the way to handle that would be to initially create a clone of the filtered select element and manipulate this clone.

Something like this, which will also handle returning to the page:
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">
 
  $(document).on('ready pjax:scriptcomplete', function() {
 
    // Identify some elements
    var qID = '{QID}';
    var thisQuestion = $('#question'+qID);
    var filterQuestion = thisQuestion.prevAll('.question-container:eq(0)');
    var thisAnswerItem = $('.answer-item', thisQuestion);
    var filterSelect = $('select.form-control:eq(0)', filterQuestion); 
 
    // Define the included options
    var includes = {
      'A1': ['AO01', 'AO02', 'AO03', 'AO04', 'AO05', 'AO06', 'AO07', 'AO08', 'AO09', 'AO10', 'AO11', 'AO12', 'AO13', 'AO14', 'AO15', 'AO16', 'AO17', 'AO18', 'AO19', 'AO20', 'AO21', 'AO22'], // Bau
      'A2': ['AO28', 'AO29', 'AO30', 'AO31', 'AO32', 'AO33', 'AO34', 'AO35', 'AO36', 'AO37', 'AO38', 'AO39', 'AO40', 'AO41', 'AO42', 'AO43', 'AO44', 'AO45'], // Elektro
      'A3': ['AO83', 'AO84', 'AO85', 'AO86', 'AO87', 'AO88', 'AO89', 'AO90', 'AO91', 'AO92', 'AO93', 'AO94'], // Metall
      'A4': ['AO95', 'AO96', 'AO97', 'AO98', 'AO99', 'AO100', 'AO101', 'AO102', 'AO103'], // Sanitär-, Heizung-, Klimatechnik
      'A5': ['AO77', 'AO78', 'AO79', 'AO80', 'AO81', 'AO82'], // Holz
      'A6': ['AO66', 'AO67', 'AO68', 'AO69', 'AO70', 'AO71', 'AO72', 'AO73'], // Farbe und Gestaltung
      'A7': ['AO46', 'AO47', 'AO48', 'AO49', 'AO50', 'AO51', 'AO52', 'AO53', 'AO54', 'AO55', 'AO56', 'AO57', 'AO58', 'AO59', 'AO60', 'AO61', 'AO62', 'AO63', 'AO64', 'AO65'], // Fahrzeuge
      'A8': ['AO74', 'AO75', 'AO76'], // Friseur und Kosmetik
      'A9': ['AO23', 'AO24', 'AO25', 'AO26', 'AO27'] // Büro und Verwaltung
    };
 
    // Define the "Please choose..." text
    var chooseText = "Please choose..."
 
    // Create a clone of the filtered select
    var thisSelectClone = $('select.form-control:eq(0)', thisQuestion).clone();
 
    // A function to handle the filtered select
    function handleSelect(init) {
 
      var thisVal = '';
      if(init) {
        thisVal = $('select.form-control:eq(0)', thisAnswerItem).val();
      }
 
      // Remove the filtered select
      $('select.form-control:eq(0)', thisAnswerItem).remove();
 
      // Insert a new cloned select
      $(thisAnswerItem).prepend(thisSelectClone.clone());
 
      // Get the value of the previous question
      var selectedValue = $(filterSelect).val();
 
      // Identify the relevant select options
      $.each(includes[selectedValue], function(i, val) {
        $('select.form-control:eq(0) option[value="'+val+'"]', thisAnswerItem).addClass('relevant-item');
      });
      $('select.form-control:eq(0) option[value="-oth-"]', thisAnswerItem).addClass('relevant-item');
 
      // Remove irrelevant select options
      $('select.form-control:eq(0) option:not(.relevant-item)', thisAnswerItem).remove();
 
      // Insert the "Please choose..." option
      if(thisVal == '') {
        $('select.form-control:eq(0)', thisAnswerItem).prepend('<option value="">'+chooseText+'</option>');
      }
 
      // Set the value of the new cloned select
      $('select.form-control:eq(0)', thisAnswerItem).val(thisVal).trigger('change');
    }
 
    // Listener on the filtering select
    $(filterSelect).on('change', function(e) {
      handleSelect();
    });
 
    // Returning to the page
    if(filterSelect.val() != '') {
      handleSelect(true);
    }
  });
</script>

Here is your sample survey back with the middle question removed and the script above in the source of "A1":  

File Attachment:

File Name: limesurvey...2611.lss
File Size:93 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 months 2 weeks ago by tpartner.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose