- Posts: 21
- 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 the questions var thisQuestion = $('#question{QID}'); var nextQuestion = $(thisQuestion).next('div[id^="question"]'); // Hide the multi-text question $(nextQuestion).hide(); // Add extra cells to the array rows $('.subquestions-list thead tr', thisQuestion).append('<th />'); $('.subquestions-list tbody tr', thisQuestion).append('<td />'); // Move the multi-text question text to the last column header cell of the array $('.subquestions-list thead tr th:last', thisQuestion).text($('.questiontext', nextQuestion).text()); // Move the text inputs $('input.text', nextQuestion).each(function(i){ $('.subquestions-list tbody tr:eq('+i+') td:last', thisQuestion).append(this); }); // Some cleanup styles $('col', thisQuestion).css({ 'width': 'auto' }); $('.subquestions-list tbody th, .subquestions-list tbody td', thisQuestion).css({ 'padding': '4px 10px' }); }); </script>
tpartner wrote: Can you attach a small sample survey containing only that group? What LS version are you using?
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify the questions var thisQuestion = $('#question{QID}'); var nextQuestion = $(thisQuestion).next('div[id^="question"]'); // Hide the multi-text question $(nextQuestion).hide(); // Add extra cells to the array rows $('.subquestions-list thead tr', thisQuestion).append('<th />'); $('.subquestions-list tbody tr', thisQuestion).append('<td />'); // Move the multi-text question text to the last column header cell of the array $('.subquestions-list thead tr th:last', thisQuestion).text($('.questiontext', nextQuestion).text()); // Move the text inputs $('input.text', nextQuestion).each(function(i){ $('.subquestions-list tbody tr:eq('+i+') td:last', thisQuestion).append(this); }); // Some cleanup styles $('col', thisQuestion).css({ 'width': 'auto' }); $('.subquestions-list tbody th, .subquestions-list tbody td', thisQuestion).css({ 'padding': '4px 10px' }); // Assign classes $('input.text', thisQuestion).closest('td').prev('td').find('input.radio').addClass('other-radio'); // Initial states $('input.text', thisQuestion).prop('disabled', true); $('input.other-radio:checked', thisQuestion).closest('tr').find('input.text').prop('disabled', false) // Listener on the radios $('input.radio', thisQuestion).on('click', function(e) { var thisRow = $(this).closest('tr'); if($(this).hasClass('other-radio')) { $('input.text', thisRow).prop('disabled', false).focus(); } else { $('input.text', thisRow).prop('disabled', true).val(''); } }); // Interrupt the Next/Submit click $('#movenextbtn, #movesubmitbtn').on('click', function () { var nextOK = true; $('input.other-radio:checked', thisQuestion).each(function(i) { if($.trim($(this).closest('tr').find('input.text').val()) == '') { nextOK = false; } }); if(nextOK == false) { alert('All "Other" items must have a description.'); return false; } else { $('input.text', thisQuestion).prop('disabled', false); } }); }); </script>
tpartner wrote: Yes, you can put a listener on the radio inputs to enable/disable the corresponding text inputs. Something like this (including your code):
Code:<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify the questions var thisQuestion = $('#question{QID}'); var nextQuestion = $(thisQuestion).next('div[id^="question"]'); // Hide the multi-text question $(nextQuestion).hide(); // Add extra cells to the array rows $('.subquestions-list thead tr', thisQuestion).append('<th />'); $('.subquestions-list tbody tr', thisQuestion).append('<td />'); // Move the multi-text question text to the last column header cell of the array $('.subquestions-list thead tr th:last', thisQuestion).text($('.questiontext', nextQuestion).text()); // Move the text inputs $('input.text', nextQuestion).each(function(i){ $('.subquestions-list tbody tr:eq('+i+') td:last', thisQuestion).append(this); }); // Some cleanup styles $('col', thisQuestion).css({ 'width': 'auto' }); $('.subquestions-list tbody th, .subquestions-list tbody td', thisQuestion).css({ 'padding': '4px 10px' }); // Assign classes $('input.text', thisQuestion).closest('td').prev('td').find('input.radio').addClass('other-radio'); // Initial states $('input.text', thisQuestion).prop('disabled', true); $('input.other-radio:checked', thisQuestion).closest('tr').find('input.text').prop('disabled', false) // Listener on the radios $('input.radio', thisQuestion).on('click', function(e) { var thisRow = $(this).closest('tr'); if($(this).hasClass('other-radio')) { $('input.text', thisRow).prop('disabled', false).focus(); } else { $('input.text', thisRow).prop('disabled', true).val(''); } }); // Interrupt the Next/Submit click $('#movenextbtn, #movesubmitbtn').on('click', function () { var nextOK = true; $('input.other-radio:checked', thisQuestion).each(function(i) { if($.trim($(this).closest('tr').find('input.text').val()) == '') { nextOK = false; } }); if(nextOK == false) { alert('All "Other" items must have a description.'); return false; } else { $('input.text', thisQuestion).prop('disabled', false); } }); }); </script>
Here is your survey back with that modification.
// Listener on the checkboxes $('input.checkbox.other-radio', thisQuestion).on('change', function(e) { var thisRow = $(this).closest('tr'); if($(this).is(':checked')) { $('input.text', thisRow).prop('disabled', false).focus(); } else { $('input.text', thisRow).prop('disabled', true).val(''); } });