Welcome to the LimeSurvey Community Forum

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

Filtering answers options in list dropdown maintaining other option

  • lfanfoni
  • lfanfoni's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
6 years 4 months ago #161005 by lfanfoni
I need to filter answers options in a list dropdown, using a equation question in a previous page or a token attribute.
I have to keep the 'other' option in every case.
I thought to adopt this solution: manual.limesurvey.org/Workarounds:_Manip..._Javascript#Method_2
filtering the first two characters of options code. But I cannot refer to a question in a previos page or a token attribute, and i don't know how to keep the standard other option.
I'm using current 2.7X.
Please help me to achieve this. Thanks a lot
Luciano

Se mi vuoi contattare, non mandare messaggi privati qui, bensì scrivi a lfanfoni at gmail.com
Per le livestream in italiano vai su: www.youtube.com/playlist?list=PLOSjjxAG9...SiiCsSz_JxIH7xJwLdPd
Le soluzioni LimeSurvey per l'Italia si trovano su: github.com/lfanfoni
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161008 by tpartner
If you want to filter a list-dropdown by the value of a previous equation question, you can use a script something like below.

This script gets the two-character value of a previous equation question with code "Q2" and removes all answer options that do not have codes starting with those two characters.

Code:
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    var chooseText = 'Please choose...';
 
    // Get the filtering value
    var filterValue = '{Q2}';
 
    // Handle previous answers
    var selectVal = $.trim($('.answer-item select', thisQuestion).val());
    if(selectVal != '' &amp;&amp; selectVal.substr(0, 2).toUpperCase() != filterValue) {
      $('.answer-item select', thisQuestion).prepend('<option value="">'+chooseText+'</option>').val('').trigger('change');
    }
 
    // Loop through all dropdown options and remove those with codes not starting with the filterValue
    $('.answer-item select option', thisQuestion).each(function(i){       
      if($(this).attr('value')  &amp;&amp; $(this).attr('value').substr(0, 2).toUpperCase() != filterValue) {
        $(this).remove();
      }
    });
  });
</script>

The script would be similar for filtering by a token attribute but you would replace:
Code:
var filterValue = '{Q2}';
With something like:
Code:
var filterValue = '{TOKEN:ATTRIBUTE_1}';

Sample survey attached:

File Attachment:

File Name: limesurvey...1-25.lss
File Size:19 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: lfanfoni
The topic has been locked.
  • lfanfoni
  • lfanfoni's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
6 years 4 months ago #161011 by lfanfoni
Thanks a lot. How can I to avoide to remove the standard 'other' option in the dropdown?

Se mi vuoi contattare, non mandare messaggi privati qui, bensì scrivi a lfanfoni at gmail.com
Per le livestream in italiano vai su: www.youtube.com/playlist?list=PLOSjjxAG9...SiiCsSz_JxIH7xJwLdPd
Le soluzioni LimeSurvey per l'Italia si trovano su: github.com/lfanfoni
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161013 by tpartner
Oops, sorry, forgot that requirement.

Code:
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    var chooseText = 'Please choose...';
 
    // Get the filtering value
    var filterValue = '{Q2}';
 
    // Handle previous answers
    var selectVal = $.trim($('.answer-item select', thisQuestion).val());
    if(selectVal != '' &amp;&amp; selectVal != '-oth-' &amp;&amp; selectVal.substr(0, 2).toUpperCase() != filterValue) {
      $('.answer-item select', thisQuestion).prepend('<option value="">'+chooseText+'</option>').val('').trigger('change');
    }
 
    // Loop through all dropdown options and remove those with codes not starting with the filterValue
    $('.answer-item select option', thisQuestion).each(function(i){       
      if($(this).attr('value') &amp;&amp; $(this).attr('value') != '-oth-'  &amp;&amp; $(this).attr('value').substr(0, 2).toUpperCase() != filterValue) {
        $(this).remove();
      }
    });
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...25-2.lss
File Size:19 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: lfanfoni
The topic has been locked.
  • lfanfoni
  • lfanfoni's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
6 years 4 months ago #161020 by lfanfoni
It works perfectly!
Eternal gratitude!

Luciano

Se mi vuoi contattare, non mandare messaggi privati qui, bensì scrivi a lfanfoni at gmail.com
Per le livestream in italiano vai su: www.youtube.com/playlist?list=PLOSjjxAG9...SiiCsSz_JxIH7xJwLdPd
Le soluzioni LimeSurvey per l'Italia si trovano su: github.com/lfanfoni
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose