- Posts: 22
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Alternate ranking question - Dynamic answer options
- Gensz78
- Topic Author
- Offline
- Junior Member
Less
More
6 years 4 months ago #159909
by Gensz78
Replied by Gensz78 on topic Alternate ranking question - Dynamic answer options
No problem, thanks for your help!
Gen
Gen
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
6 years 4 months ago #159910
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 Alternate ranking question - Dynamic answer options
Try this with all extra spaces removed:
Code:
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:complete',function() { // Identify this question var thisQuestion = $('#question{QID}'); // Define some "Choose" text var choosetext = 'Please choose...'; // Number of visible rows var rowCount = $('tr.answer-item:visible', thisQuestion).length; // Loop through all drop-downs $('tr.answer-item select', thisQuestion).each(function(i) { // Reset if value is above the number of visible rows if($(this).val() > rowCount) { $(this).prepend('<option value="">'+choosetext+'</option>').val(''); checkconditions(this.value, this.name, this.type); } // Remove all unnecessary options $('option', this).filter(function() { return this.value > rowCount; }).remove(); }); // Listener on the dropdowns $('tr.answer-item select', thisQuestion).on('change', function(e) { // Handle non-unique answers handleDuplicates(); }); function handleDuplicates() { $('.question-item', thisQuestion).removeClass('duplicate-row'); $('tr.answer-item select', thisQuestion).each(function(i) { if(!$(this).closest('.question-item').hasClass('duplicate-row') && $(this).val() != '') { var thisSelect = $(this); var selectedValue = $(this).val(); $('tr.answer-item select', thisQuestion).not(this).each(function(i) { if($(this).val() == selectedValue) { $(this).closest('.question-item').addClass('duplicate-row'); $(thisSelect).closest('.question-item').addClass('duplicate-row'); } }); } }); } handleDuplicates(); //Insert new style rules var newStyle = '<style type="text/css">\ .duplicate-row {\ background-color: #E74C3C;\ }\ .duplicate-row .answertext {\ color: #FFFFFF;\ }\ </style>'; $("head link[rel='stylesheet']").last().after(newStyle); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- Gensz78
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 22
- Thank you received: 0
6 years 4 months ago #159911
by Gensz78
Replied by Gensz78 on topic Alternate ranking question - Dynamic answer options
Oh wow, thank you! This removes the unneeded rankings and marks duplicates in red! But the filtering of the behavOtherIDENTIFY question is still broken.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
6 years 4 months ago #159920
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 Alternate ranking question - Dynamic answer options
Oh, I see your problem now - the filtering is working but the piping is not. Something in the style declaration is messing with Expression Manager.
Use this script. Note that in the style rules you must leave a space after the opening curly braces and before the closing braces.
Working survey attached:
Use this script. Note that in the style rules you must leave a space after the opening curly braces and before the closing braces.
Code:
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:complete',function() { // Identify this question var thisQuestion = $('#question{QID}'); // Define some "Choose" text var choosetext = 'Please choose...'; // Number of visible rows var rowCount = $('tr.answer-item:visible', thisQuestion).length; // Loop through all drop-downs $('tr.answer-item select', thisQuestion).each(function(i) { // Reset if value is above the number of visible rows if($(this).val() > rowCount) { $(this).prepend('<option value="">'+choosetext+'</option>').val(''); checkconditions(this.value, this.name, this.type); } // Remove all unnecessary options $('option', this).filter(function() { return this.value > rowCount; }).remove(); }); // Listener on the dropdowns $('tr.answer-item select', thisQuestion).on('change', function(e) { // Handle non-unique answers handleDuplicates(); }); function handleDuplicates() { $('.question-item', thisQuestion).removeClass('duplicate-row'); $('tr.answer-item select', thisQuestion).each(function(i) { if(!$(this).closest('.question-item').hasClass('duplicate-row') && $(this).val() != '') { var thisSelect = $(this); var selectedValue = $(this).val(); $('tr.answer-item select', thisQuestion).not(this).each(function(i) { if($(this).val() == selectedValue) { $(this).closest('.question-item').addClass('duplicate-row'); $(thisSelect).closest('.question-item').addClass('duplicate-row'); } }); } }); } handleDuplicates(); //Insert new style rules var newStyle = '<style type="text/css">\ .duplicate-row { background-color: #E74C3C; }\ .duplicate-row .answertext { color: #FFFFFF; }\ </style>'; $("head link[rel='stylesheet']").last().after(newStyle); }); </script>
Working survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
- Gensz78
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 22
- Thank you received: 0
6 years 4 months ago #159946
by Gensz78
Replied by Gensz78 on topic Alternate ranking question - Dynamic answer options
Thank you so much! It's working beautifully now!
Gen
Gen
The topic has been locked.