- Posts: 28
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
//Define column and limit for each var checkboxConfig = [ {name: col1, limit: 3}, {name: col2, limit: 3}, {name: col3, limit: 3}, {name: col4, limit: 3} ]; //Set variable for array as a whole for (i = 0; i < checkboxConfig.length; i += 1) { setLimit(checkboxConfig[i].name, checkboxConfig[i].limit); } //Assign Click Handler for each checkbox function setLimit(name, limit) { var els = document.getElementsByName(name); for (i = 0; i<els.length; i += 1) { els[i].onclick = checkboxClickHandler(els[i], limit); } } //New function for each click handler function checkboxClickHandler(el, limit) { return function () { limit_checkbox(el, limit); } } //Limit checkbox function function limit_checkbox(el, max) { var count = 0, i; var checkboxes = document.getElementsByName(el.name); for (i = 0; i < checkboxes.length; i += 1) { if (checkboxes[i].checked) { count = count + 1; } } if (count > max) { alert('Please select only ' + max + ' checkboxes.\ To select this option unselect one of the others.'); el.checked = false; } }
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { limit_checkbox(‘{el}’,[‘col1’,’ col2’,’ col3’,’ col4’]) }); var col1 = {688526X267X7389SQ001_SQ001,688526X267X7389SQ002_SQ001,688526X267X7389SQ003_SQ001,688526X267X7389SQ004_SQ001}; var col2 = {688526X267X7389SQ001_SQ002,688526X267X7389SQ002_SQ002,688526X267X7389SQ003_SQ002,688526X267X7389SQ004_SQ002}; var col3 = {688526X267X7389SQ001_SQ003,688526X267X7389SQ002_SQ003,688526X267X7389SQ003_SQ003,688526X267X7389SQ004_SQ003}; var col4 = {688526X267X7389SQ001_SQ004,688526X267X7389SQ002_SQ004,688526X267X7389SQ003_SQ004,688526X267X7389SQ004_SQ004}; </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Define the maximum answers per column var maxAnswers = 3; // Identify this question var thisQuestion = $('#question{QID}'); // Index the array columns $('table.subquestion-list tr', thisQuestion).each(function(i) { $('> *', this).each(function(i) { $(this).attr('data-index', i); }); }); // Listener on the checkboxes $('input[type="checkbox"]', thisQuestion).on('change', function(e) { var thisIndex = $(this).closest('.answer-item').attr('data-index'); $('[data-index="'+thisIndex+'"] input[type="checkbox"]', thisQuestion).prop('disabled', false); // If max reached, disable unchecked inputs in this column if($('[data-index="'+thisIndex+'"] input[type="checkbox"]:checked', thisQuestion).length >= maxAnswers) { $('[data-index="'+thisIndex+'"] input[type="checkbox"]:not(:checked)', thisQuestion).prop('disabled', true); } }); }); </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // The answer code to place in the last position var fixedCode = 'SQ004'; // Identify this question var q1ID = {QID}; var thisQuestion = $('#question'+q1ID); // Move the "fixed" row to the end $('multiflexible_checkbox', thisQuestion).append($('div[id^="javatbd"][id$="X'+q1ID+fixedCode+'"]', thisQuestion)); }); </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Define the maximum answers per column var maxAnswers = 3; // Identify this question var thisQuestion = $('#question{QID}'); // Index the array columns $('table.subquestion-list tr', thisQuestion).each(function(i) { $('> *', this).each(function(i) { $(this).attr('data-index', i); }); }); // Listener on the checkboxes $('input[type="checkbox"]', thisQuestion).on('change', function(e) { var thisIndex = $(this).closest('.answer-item').attr('data-index'); $('[data-index="'+thisIndex+'"] input[type="checkbox"]', thisQuestion).prop('disabled', false); // If max reached, disable unchecked inputs in this column if($('[data-index="'+thisIndex+'"] input[type="checkbox"]:checked', thisQuestion).length >= maxAnswers) { $('[data-index="'+thisIndex+'"] input[type="checkbox"]:not(:checked)', thisQuestion).prop('disabled', true); } }); // The sub-question code to place in the last position var fixedCode = 'Y004'; // Move the "fixed" row to the end $('table.subquestion-list tbody:eq(0), table.subquestions-list tbody:eq(0)', thisQuestion).append($('tr[id$="X'+{QID}+fixedCode +'"]')); // Fix up the row background colours $('table.subquestion-list tbody tr, table.subquestions-list tbody tr', thisQuestion).each(function(i){ $(this).removeClass('array1, array2'); if(i % 2 == 0) { $(this).addClass('array1'); } else { $(this).addClass('array2'); } }); }); </script>