- Posts: 10109
- Thank you received: 3595
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Unchecking excluded checkboxes
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
5 years 11 months ago #168095
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 Unchecking excluded checkboxes
This will work in 3.x:
Sample survey attached:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Add some classes $('.question-item:not(:last)', thisQuestion).addClass('non-exclusive-item'); $('.question-item:last', thisQuestion).addClass('exclusive-item'); // Handle exclusive items $('input[type="checkbox"]', thisQuestion).on('change', function(e) { if($(this).is(':checked')) { var actionItems = $('.non-exclusive-item', thisQuestion); if($(this).closest('.question-item').hasClass('non-exclusive-item')) { actionItems = $('.exclusive-item', thisQuestion); } actionItems.each(function(i) { $('input[type="checkbox"]', this).prop('checked', false).trigger('change'); $('input:hidden', this).attr('value', ''); $('input[type="text"]', this).val('').trigger('keyup'); }); } }); $('input[type="text"]', thisQuestion).on('keyup change', function(e) { var thisInput = $(this); setTimeout(function() { $(thisInput).closest('.question-item').find('input[type="checkbox"]').trigger('change'); }, 200); }); }); </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 topic has been locked.
- Andrea01
- Offline
- Senior Member
Less
More
- Posts: 64
- Thank you received: 0
5 years 9 months ago #171282
by Andrea01
Replied by Andrea01 on topic Unchecking excluded checkboxes
Hi Tony,
for your script:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Identify this question
var thisQuestion = $('#question{QID}');
// Add some classes
$('.question-item:not(:last)', thisQuestion).addClass('non-exclusive-item');
$('.question-item:last', thisQuestion).addClass('exclusive-item');
// Handle exclusive items
$('input.checkbox', thisQuestion).on('change', function(e) {
if($(this).is(':checked')) {
var actionItems = $('.non-exclusive-item', thisQuestion);
if($(this).closest('.question-item').hasClass('non-exclusive-item')) {
actionItems = $('.exclusive-item', thisQuestion);
}
actionItems.each(function(i) {
$('input.checkbox', this).prop('checked', false).trigger('change');
$('input:hidden', this).attr('value', '');
$('input[type="text"]', this).val('').trigger('keyup');
});
}
});
});
</script>
Is there a way to handle two exclusive options???
Thanks and best regards
Andrea
for your script:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Identify this question
var thisQuestion = $('#question{QID}');
// Add some classes
$('.question-item:not(:last)', thisQuestion).addClass('non-exclusive-item');
$('.question-item:last', thisQuestion).addClass('exclusive-item');
// Handle exclusive items
$('input.checkbox', thisQuestion).on('change', function(e) {
if($(this).is(':checked')) {
var actionItems = $('.non-exclusive-item', thisQuestion);
if($(this).closest('.question-item').hasClass('non-exclusive-item')) {
actionItems = $('.exclusive-item', thisQuestion);
}
actionItems.each(function(i) {
$('input.checkbox', this).prop('checked', false).trigger('change');
$('input:hidden', this).attr('value', '');
$('input[type="text"]', this).val('').trigger('keyup');
});
}
});
});
</script>
Is there a way to handle two exclusive options???
Thanks and best regards
Andrea
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3595
5 years 9 months ago #171315
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 Unchecking excluded checkboxes
This script will render the last two items exclusive:
Sample survey attached:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Add some classes var sqCount = $('.question-item', thisQuestion).length; $('.question-item', thisQuestion).addClass('non-exclusive-item'); $('.question-item:eq('+(sqCount-2)+')', thisQuestion).removeClass('non-exclusive-item').addClass('exclusive-item'); // 2nd last item $('.question-item:eq('+(sqCount-1)+')', thisQuestion).removeClass('non-exclusive-item').addClass('exclusive-item'); // Last item // Handle exclusive items $('input[type="checkbox"]', thisQuestion).on('change', function(e) { if($(this).is(':checked')) { var thisItem = $(this).closest('.question-item'); var actionItems = $('.question-item', thisQuestion).not($(thisItem)); if($(thisItem).hasClass('non-exclusive-item')) { actionItems = $('.exclusive-item', thisQuestion); } actionItems.each(function(i) { $('input[type="checkbox"]', this).prop('checked', false).trigger('change'); $('input:hidden', this).attr('value', ''); $('input[type="text"]', this).val('').trigger('keyup'); }); } }); $('input[type="text"]', thisQuestion).on('keyup change', function(e) { var thisInput = $(this); setTimeout(function() { $(thisInput).closest('.question-item').find('input[type="checkbox"]').trigger('change'); }, 200); }); }); </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 topic has been locked.
- Andrea01
- Offline
- Senior Member
Less
More
- Posts: 64
- Thank you received: 0
5 years 9 months ago #171340
by Andrea01
Replied by Andrea01 on topic Unchecking excluded checkboxes
Thank you Tony, perfect as always.
Andrea
Andrea
The topic has been locked.
- rizkiheryandi
- Offline
- New Member
Less
More
- Posts: 19
- Thank you received: 1
5 years 4 months ago #178675
by rizkiheryandi
Thanks it's works for latest Limesurvey
Replied by rizkiheryandi on topic Unchecking excluded checkboxes
tpartner wrote: This will work in 3.x:
Code:<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Add some classes $('.question-item:not(:last)', thisQuestion).addClass('non-exclusive-item'); $('.question-item:last', thisQuestion).addClass('exclusive-item'); // Handle exclusive items $('input[type="checkbox"]', thisQuestion).on('change', function(e) { if($(this).is(':checked')) { var actionItems = $('.non-exclusive-item', thisQuestion); if($(this).closest('.question-item').hasClass('non-exclusive-item')) { actionItems = $('.exclusive-item', thisQuestion); } actionItems.each(function(i) { $('input[type="checkbox"]', this).prop('checked', false).trigger('change'); $('input:hidden', this).attr('value', ''); $('input[type="text"]', this).val('').trigger('keyup'); }); } }); $('input[type="text"]', thisQuestion).on('keyup change', function(e) { var thisInput = $(this); setTimeout(function() { $(thisInput).closest('.question-item').find('input[type="checkbox"]').trigger('change'); }, 200); }); }); </script>
Sample survey attached:
Thanks it's works for latest Limesurvey
The topic has been locked.