- Posts: 916
- Thank you received: 218
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
- Topic Author
- Offline
- Platinum Member
Less
More
6 years 4 months ago #161005
by lfanfoni
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
Filtering answers options in list dropdown maintaining other option was created 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
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
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
6 years 4 months ago #161008
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Filtering answers options in list dropdown maintaining other option
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.
The script would be similar for filtering by a token attribute but you would replace:
With something like:
Sample survey attached:
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 != '' && 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') && $(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}';
Code:
var filterValue = '{TOKEN:ATTRIBUTE_1}';
Sample survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The following user(s) said Thank You: lfanfoni
The topic has been locked.
- lfanfoni
- Topic Author
- Offline
- Platinum Member
Less
More
- Posts: 916
- Thank you received: 218
6 years 4 months ago #161011
by lfanfoni
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
Replied by lfanfoni on topic Filtering answers options in list dropdown maintaining other option
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
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
6 years 4 months ago #161013
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Filtering answers options in list dropdown maintaining other option
Oops, sorry, forgot that requirement.
Sample survey attached:
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 != '' && selectVal != '-oth-' && 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') && $(this).attr('value') != '-oth-' && $(this).attr('value').substr(0, 2).toUpperCase() != filterValue) { $(this).remove(); } }); }); </script>
Sample survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The following user(s) said Thank You: lfanfoni
The topic has been locked.
- lfanfoni
- Topic Author
- Offline
- Platinum Member
Less
More
- Posts: 916
- Thank you received: 218
6 years 4 months ago #161020
by lfanfoni
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
Replied by lfanfoni on topic Filtering answers options in list dropdown maintaining other option
It works perfectly!
Eternal gratitude!
Luciano
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.