- Posts: 5
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // Identify this question var thisQuestion = $('#question{QID}'); // Assign column-specific classes $('table.subquestion-list tr', thisQuestion).each(function(i) { $('> *:gt(0)', this).each(function(i){ $(this).addClass('column-'+(i+1)); $(this).attr('data-column', i+1); }); }); // Hide the text inputs in columns 3 and 4 $('.column-3 input[type="text"], .column-4 input[type="text"]', thisQuestion).hide(); // Define the select element (dropdown) var select1 = '<select class="inserted-select"> \ <option value="">-- Please Choose --</option> \ <option value="Program 1">Program 1</option> \ <option value="Program 2">Program 2</option> \ <option value="Program 3">Program 3</option> \ <option value="Program 4">Program 4</option> \ </select>'; // Insert the select elements into column 3 $('.answer-item.column-3').append(select1); // Initial dropdown values in column 3 (if the question has already been answered) $('.answer-item.column-3 input[type="text"]').each(function(i){ if($.trim($(this).val()) != '') { $(this).closest('td').find('.inserted-select').val($.trim($(this).val())); } }); // Listener on the dropdowns (insert selected values into hidden text input) $('.inserted-select').change(function() { $(this).closest('td').find('input[type="text"]').val($(this).val()); }); // Insert the checkboxes into column 4 $('.answer-item.column-4').append('<input class="checkbox inserted-checkbox" type="checkbox" />'); // Initial checkbox states (if the question has already been answered) $('.answer-item.column-4 input[type="text"]').each(function(i){ if($.trim($(this).val()) == 'Y') { $(this).closest('td').find('.inserted-checkbox').attr('checked', true); } }); // Listener on the checkboxes (insert "Y" into hidden text input when checked) $('.inserted-checkbox').change(function() { if($(this).is(':checked')) { $(this).closest('td').find('input[type="text"]').val('Y'); } else { $(this).closest('td').find('input[type="text"]').val(''); } }); }); </script>
tpartner wrote: Okay, here's a JavaScript solution. In this example, drop-downs are inserted into column 3 and check-boxes into column 4. Note that these are true check-boxes, not the pseudo-elements inserted into some 2.5 templates.
Code:<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // Identify this question var thisQuestion = $('#question{QID}'); // Assign column-specific classes $('table.subquestion-list tr', thisQuestion).each(function(i) { $('> *:gt(0)', this).each(function(i){ $(this).addClass('column-'+(i+1)); $(this).attr('data-column', i+1); }); }); // Hide the text inputs in columns 3 and 4 $('.column-3 input[type="text"], .column-4 input[type="text"]', thisQuestion).hide(); // Define the select element (dropdown) var select1 = '<select class="inserted-select"> \ <option value="">-- Please Choose --</option> \ <option value="Program 1">Program 1</option> \ <option value="Program 2">Program 2</option> \ <option value="Program 3">Program 3</option> \ <option value="Program 4">Program 4</option> \ </select>'; // Insert the select elements into column 3 $('.answer-item.column-3').append(select1); // Initial dropdown values in column 3 (if the question has already been answered) $('.answer-item.column-3 input[type="text"]').each(function(i){ if($.trim($(this).val()) != '') { $(this).closest('td').find('.inserted-select').val($.trim($(this).val())); } }); // Listener on the dropdowns (insert selected values into hidden text input) $('.inserted-select').change(function() { $(this).closest('td').find('input[type="text"]').val($(this).val()); }); // Insert the checkboxes into column 4 $('.answer-item.column-4').append('<input class="checkbox inserted-checkbox" type="checkbox" />'); // Initial checkbox states (if the question has already been answered) $('.answer-item.column-4 input[type="text"]').each(function(i){ if($.trim($(this).val()) == 'Y') { $(this).closest('td').find('.inserted-checkbox').attr('checked', true); } }); // Listener on the checkboxes (insert "Y" into hidden text input when checked) $('.inserted-checkbox').change(function() { if($(this).is(':checked')) { $(this).closest('td').find('input[type="text"]').val('Y'); } else { $(this).closest('td').find('input[type="text"]').val(''); } }); }); </script>
Sample survey attached:
// Insert the checkboxes into column 4 $('.answer-item.column-4').append('<input class="checkbox inserted-checkbox" type="checkbox" />').css({ 'position': 'absolute', 'left': '-9999em' });