- Posts: 10216
- Thank you received: 3635
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var thisQuestion = $('#question{QID}'); // Column-specific classes $('tr.subquestion-list', thisQuestion).each(function(i) { $('th, td', this).each(function(i) { $(this).addClass('column-'+i); }); }); // Insert checkboxes $('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3, .answer-item.column-4, .answer-item.column-7, .answer-item.column-8', thisQuestion).addClass('custom-checkbox-item'); $('.custom-checkbox-item', thisQuestion).each(function(i) { var thisID = $('input:text:eq(0)', this).attr('id'); $('label', this).before('<input class="" id="'+thisID+'" value="Y" type="checkbox" name="'+thisID.replace(/answer/, '')+'" />'); if($('input:text:eq(0)', this).val() == 'Y') { $('input:checkbox:eq(0)', this).prop('checked', true); } $(this).removeClass('text-item').addClass('checkbox-item'); $('input:text:eq(0)', this).remove(); }); // Identify exclusive items $('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3, .answer-item.column-4, .answer-item.column-5, .answer-item.column-6', thisQuestion).addClass('non-exclusive-item'); $('.answer-item.column-7, .answer-item.column-8', thisQuestion).addClass('exclusive-item'); // Listeners for exclusive items $('.non-exclusive-item input:checkbox', thisQuestion).on('change', function(e) { if($(this).is(':checked')) { $(this).closest('tr.subquestion-list').find('.exclusive-item input:checkbox').prop('checked', false); } }); $('.non-exclusive-item input:text', thisQuestion).on('keyup change', function(e) { if($.trim($(this).val()) != '') { $(this).closest('tr.subquestion-list').find('.exclusive-item input:checkbox').prop('checked', false); } }); $('.exclusive-item input:checkbox', thisQuestion).on('change', function(e) { if($(this).is(':checked')) { var thisItem = $(this).closest('.answer-item'); $(this).closest('tr.subquestion-list').find('.answer-item').not(thisItem).find('input:checkbox').prop('checked', false); $(this).closest('tr.subquestion-list').find('input:text').val(''); } }); }); </script>
tpartner wrote: I would be inclined to use an array-texts type question and swap out some of the text inputs for checkbox inputs.
Add a script like this to the question source. The check-boxes will give a value of "Y" in the data when checked.
My time is limited this week so I will leave it to you to test the data and, perhaps, adjust styling.
Code:<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var thisQuestion = $('#question{QID}'); // Column-specific classes $('tr.subquestion-list', thisQuestion).each(function(i) { $('th, td', this).each(function(i) { $(this).addClass('column-'+i); }); }); // Insert checkboxes $('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3, .answer-item.column-4, .answer-item.column-7, .answer-item.column-8', thisQuestion).addClass('custom-checkbox-item'); $('.custom-checkbox-item', thisQuestion).each(function(i) { var thisID = $('input:text:eq(0)', this).attr('id'); $('label', this).before('<input class="" id="'+thisID+'" value="Y" type="checkbox" name="'+thisID.replace(/answer/, '')+'" />'); if($('input:text:eq(0)', this).val() == 'Y') { $('input:checkbox:eq(0)', this).prop('checked', true); } $(this).removeClass('text-item').addClass('checkbox-item'); $('input:text:eq(0)', this).remove(); }); // Identify exclusive items $('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3, .answer-item.column-4, .answer-item.column-5, .answer-item.column-6', thisQuestion).addClass('non-exclusive-item'); $('.answer-item.column-7, .answer-item.column-8', thisQuestion).addClass('exclusive-item'); // Listeners for exclusive items $('.non-exclusive-item input:checkbox', thisQuestion).on('change', function(e) { if($(this).is(':checked')) { $(this).closest('tr.subquestion-list').find('.exclusive-item input:checkbox').prop('checked', false); } }); $('.non-exclusive-item input:text', thisQuestion).on('keyup change', function(e) { if($.trim($(this).val()) != '') { $(this).closest('tr.subquestion-list').find('.exclusive-item input:checkbox').prop('checked', false); } }); $('.exclusive-item input:checkbox', thisQuestion).on('change', function(e) { if($(this).is(':checked')) { var thisItem = $(this).closest('.answer-item'); $(this).closest('tr.subquestion-list').find('.answer-item').not(thisItem).find('input:checkbox').prop('checked', false); $(this).closest('tr.subquestion-list').find('input:text').val(''); } }); }); </script>
Sample survey attached:
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var thisQuestion = $('#question{QID}'); // Column-specific classes $('tr.subquestion-list', thisQuestion).each(function(i) { $('th, td', this).each(function(i) { $(this).addClass('column-'+i); }); }); // Insert checkboxes $('.answer-item.column-1', thisQuestion).addClass('custom-checkbox-item'); $('.custom-checkbox-item', thisQuestion).each(function(i) { var thisID = $('input:text:eq(0)', this).attr('id'); $('label', this).before('<input class="" id="'+thisID+'" value="Y" type="checkbox" name="'+thisID.replace(/answer/, '')+'" />'); if($('input:text:eq(0)', this).val() == 'Y') { $('input:checkbox:eq(0)', this).prop('checked', true); } $(this).removeClass('text-item').addClass('checkbox-item'); $('input:text:eq(0)', this).remove(); }); }); </script>
// Insert checkboxes $('.answer-item.column-1:lt(3)', thisQuestion).addClass('custom-checkbox-item'); ...
I'm not entirely sure where to look up the LS version I'm using, since I haven't installed LS on my computer (only using the online tool), but I'm assuming it's 3.0, because I got a message in January welcoming me to that version of the template editor.