- Posts: 26
- 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 q1ID = '{QID}'; var q1 = $('#question'+q1ID+''); var q2 = $(q1).nextAll('.array-multi-flexi-text:eq(0)'); var q2ID = $(q2).attr('id').split('question')[1]; // Initial row handling handleRows($('input[type="text"]', q1)); // Listener on the numeric input $('input[type="text"]', q1).change(function(){ var maxRows = $('tr:[id^="javatbd"]', q2).length; if($(this).val() > maxRows) { alert('You can only display a maximum of '+maxRows+' rows!'); $(this).val(''); } else { handleRows(this); } }); function handleRows(el) { var numRows = $(el).val(); $('tr:[id^="javatbd"]', q2).hide(); $('tr:[id^="javatbd"]', q2).each(function(i){ if(i >= numRows) { $('input[type="text"], select', this).val(''); } else { $(this).show(); } }) } }); </script>
$('input[type="text"]', q1).change(function(){
$('input[type="text"]', q1).mouseup(function(){
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // Identify the questions var q1ID = '{QID}'; var q1 = $('#question'+q1ID+''); var q2 = $(q1).nextAll('.array-multi-flexi-text:eq(0)'); var q2ID = $(q2).attr('id').split('question')[1]; // Initial row handling handleRows($('input[type="text"]', q1)); // Listener on the numeric input $('input[type="text"]', q1).mouseup(function(){ var maxRows = $('tr:[id^="javatbd"]', q2).length; if($(this).val() > maxRows) { alert('You can only display a maximum of '+maxRows+' rows!'); $(this).val(''); } else { handleRows(this); } }); function handleRows(el) { var numRows = $(el).val(); $('tr:[id^="javatbd"]', q2).hide(); $('tr:[id^="javatbd"]', q2).each(function(i){ if(i >= numRows) { $('input[type="text"], select', this).val(''); } else { $(this).show(); } }) } // Interrupt next/submit function $('form#limesurvey').submit(function(){ // Load hidden inputs $('.subquestions-list tr:hidden input[type="text"]', q2).val('N/A'); // Carry on with submit return true; }); }); </script>
tpartner wrote: If both questions are on the same page as your image indicates, add this to the source of the numeric question.
Code:<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // Identify the questions var q1ID = '{QID}'; var q1 = $('#question'+q1ID+''); var q2 = $(q1).nextAll('.array-multi-flexi-text:eq(0)'); var q2ID = $(q2).attr('id').split('question')[1]; // Initial row handling handleRows($('input[type="text"]', q1)); // Listener on the numeric input $('input[type="text"]', q1).change(function(){ var maxRows = $('tr:[id^="javatbd"]', q2).length; if($(this).val() > maxRows) { alert('You can only display a maximum of '+maxRows+' rows!'); $(this).val(''); } else { handleRows(this); } }); function handleRows(el) { var numRows = $(el).val(); $('tr:[id^="javatbd"]', q2).hide(); $('tr:[id^="javatbd"]', q2).each(function(i){ if(i >= numRows) { $('input[type="text"], select', this).val(''); } else { $(this).show(); } }) } }); </script>
Here's a sample survey:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ // Identify the questions var q1ID = '{QID}'; var q1 = $('#question'+q1ID+''); var q2 = $(q1).nextAll('.array-multi-flexi-text:eq(0)'); var q2ID = $(q2).attr('id').split('question')[1]; // Initial row handling handleRows($('input[type="text"]', q1)); // Listener on the numeric input $('input[type="text"]', q1).change(function(){ var maxRows = $('tr.subquestion-list', q2).length; if($(this).val() > maxRows) { alert('You can only display a maximum of '+maxRows+' rows!'); $(this).val(''); } else { handleRows(this); } }); function handleRows(el) { var numRows = $(el).val(); $('tr.subquestion-list', q2).hide(); $('tr.subquestion-list', q2).each(function(i){ if(i >= numRows) { $('input[type="text"], select', this).val(''); } else { $(this).show(); } }) } }); </script>