- Posts: 11
- Thank you received: 0
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}'); var lastInput = $('input:text:last', thisQuestion); var lastItem = $(lastInput).closest('div'); // Insert the check-box $(lastItem).addClass('checkbox-item with-inserted-checkbox').append('<input type="checkbox" name="insertedInput" id="insertedInput" value=""><label for="insertedInput" class="checkbox-label control-label"></label>'); // Listener on the checkbox $(':checkbox', lastItem).on('change', function(e) { if($(this).is(':checked')) { $('input:text', thisQuestion).not(lastInput).val('').trigger('keyup'); $(lastInput).val(100).trigger('keyup'); $('li.ls-group-dynamic', thisQuestion).hide(); } else { $('li.ls-group-dynamic', thisQuestion).show(); $(lastInput).val('').trigger('keyup'); } }); }); </script> <style type="text/css">.numeric-multi .with-inserted-checkbox input.form-control { position: absolute; left: -9999em; } .numeric-multi .with-inserted-checkbox label::before, .numeric-multi .with-inserted-checkbox label::after { margin: -7px 0 0 0; } </style>
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var thisQuestion = $('#question{QID}'); var checkboxRows = $('.answer-item:gt(2)', thisQuestion); // Insert the check-box $('> div', checkboxRows).each(function(i) { $(this).addClass('checkbox-item with-inserted-checkbox').append('<input type="checkbox" name="insertedInput'+i+'" id="insertedInput'+i+'" value=""><label for="insertedInput'+i+'" class="checkbox-label control-label"></label>'); }); // Listener on the checkbox $(':checkbox', checkboxRows).on('change', function(e) { var thisRow = $(this).closest('.answer-item'); if($(this).is(':checked')) { $('input:text', thisQuestion).val('').trigger('keyup'); $('input:text', thisRow).val(100).trigger('keyup'); $('li.ls-group-dynamic', thisQuestion).hide(); $(checkboxRows).not(thisRow).find(':checkbox').prop('checked', false); } else { $('li.ls-group-dynamic', thisQuestion).show(); $('input:text', thisRow).val('').trigger('keyup'); } }); // Listener on the text inputs $('.answer-item', thisQuestion).not(checkboxRows).find(':text').on('keyup', function(e) { $('input:text', checkboxRows).val('').trigger('keyup'); $(':checkbox', checkboxRows).prop('checked', false); $('li.ls-group-dynamic', thisQuestion).show(); }); }); </script>
<style type="text/css"> .numeric-multi .with-inserted-checkbox .ls-input-group { position: absolute; left: -9999em; } .numeric-multi .with-inserted-checkbox label::before, .numeric-multi .with-inserted-checkbox label::after { margin: -7px 0 0 0; } </style>
I found a bug, use this script:Unfortunately the checkboxes cannot be selected in your example (there are no "checked"-symbols)
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var thisQuestion = $('#question{QID}'); var checkboxRows = $('.answer-item:gt(2)', thisQuestion); // Insert the check-box $('> div', checkboxRows).each(function(i) { $(this).addClass('checkbox-item with-inserted-checkbox').append('<input type="checkbox" name="insertedInput'+i+'" id="insertedInput'+i+'" value=""><label for="insertedInput'+i+'" class="checkbox-label control-label"></label>'); }); // Listener on the checkbox $(':checkbox', checkboxRows).on('change', function(e) { var thisRow = $(this).closest('.answer-item'); if($(this).is(':checked')) { $('input:text', thisQuestion).val('').trigger('keyup'); $('input:text', thisRow).val(100).trigger('keyup'); $('li.ls-group-dynamic', thisQuestion).hide(); $(checkboxRows).not(thisRow).find(':checkbox').prop('checked', false); } else { $('li.ls-group-dynamic', thisQuestion).show(); $('input:text', thisRow).val('').trigger('keyup'); } }); // Listener on the text inputs $('.answer-item', thisQuestion).not(checkboxRows).find(':text').on('keydown change', function(e) { $('input:text', checkboxRows).val('').trigger('keyup'); $(':checkbox', checkboxRows).prop('checked', false); $('li.ls-group-dynamic', thisQuestion).show(); }); }); </script>
Then, why would you provide a sample survey with the wrong number of rows?Furthermore with "the last two items" I meant that I wanted to split last item in my original example, so in the end I would have 4 items with numeric input fields and two items with checkboxes ("Weiß nicht" and "Keine Angabe").
var checkboxRows = $('.answer-item:gt(2)', thisQuestion);
var checkboxRows = $('.answer-item:gt(3)', thisQuestion);
the code doesn't seem to work when the question is mandatory