Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Script to ask array sub questions one at a time.

  • Bigred01
  • Bigred01's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
9 years 8 months ago #110865 by Bigred01
Hi everyone. Just wanted to share this function that will let you ask array questions one at a time if you have a large list. Can help fight people straight-lining if they don't see a huge table.

Any feedback as to how it can be improved would be much appreciated.


Add this to a js file:
Code:
 
//Show array rows one at a time
arrayRowShowHide = function (qId) {
    var questionId = '#question' + qId;
 
    var ansArr = [];
 
    $(questionId + ' .answers-list').hide();
    //build array of possbile answers
    $(questionId + ' .answers-list').each(function () {
 
        ansArr.push([
            $(this).children('th').children('input').attr('id'), //sub-q input id
            $(this).attr('id') //sub-q tr id
        ]);
 
    });
    var lastAnswer = ansArr.length - 1;
    $('#' + ansArr[0][1]).show();
 
    $('.answer-item').click(function (index) {
 
  //check for and hide questions answered then show first one that's un-answered.
        $.each(ansArr, function (index) {
 
            if (document.getElementById(ansArr[index][0]).value != "") {
                $('#' + ansArr[index][1]).hide();
            }
            else {
                $('#' + ansArr[index][1]).show();
                return false;
            }
 
        });
 
  //auto next if last qub-q is answered.
        if (document.getElementById(ansArr[lastAnswer][0]).value != "") {
            $('#movenextbtn').click();
        }
    });
};
 

And you can call it with this in your question:
Code:
 
<script>
$(document).ready(function() {
 
     arrayRowShowHide('{QID}');
 
});
</script>
 
The following user(s) said Thank You: Ben_V
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose