- Posts: 6738
- Karma: 601
- Thank you received: 1807
- Forum
- English support forums
- Can I do this with LimeSurvey?
- Alternate ranking question - Dynamic answer options
Alternate ranking question - Dynamic answer options
5 months 4 weeks ago #159920
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
tpartner replied the 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.
<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.
Please Log in or Create an account to join the conversation.
5 months 4 weeks ago #159946
by Gensz78
Gensz78 replied the topic: Alternate ranking question - Dynamic answer options
Thank you so much! It's working beautifully now!
Gen
Gen
Please Log in or Create an account to join the conversation.