Welcome to the LimeSurvey Community Forum

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

multiple choice three maximum answers, two are fixed with one exclusive

  • philemmons
  • philemmons's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 9 months ago - 1 year 9 months ago #242400 by philemmons
Please help us help you and fill where relevant:
Your LimeSurvey version:  3.28.54
Own server or LimeSurvey hosting: LS hosting
Survey theme/template:  varies
==================
Hi there!

I have used this script dozens of times and this is the first time I have had this issue. When I select  answer 8, 3, 1, and 4, the JS doesn't work as intended.
Please would someone look this over and see where my bug is ????   Thank you in advance!
Code:
<script type="text/javascript" charset="utf-8">    
 
    $(document).on('ready pjax:scriptcomplete',function(){ 
 
        /* The number of answers to be fixed at the end of the list*/ 
        var fixedAnswers = 2; 
 
        /* Set this to "true" if you want "Other" to be fixed in the last position*/ 
        var otherFixed = false; 
 
        /* Identify this question*/ 
        var qID = {QID}; 
 
        /* Find the number of answers*/ 
        var ansCount = $('#question'+qID+' .answer-item').length; 
        if($('#question'+qID+' input[type="text"]').length > 0) { 
            ansCount = ansCount -1 
        } 
        console.log(ansCount); 
 
        /* Place the last n answers created at the end of the list*/ 
        var fixedIndex = fixedAnswers - 1; 
        for (var i=0; i<fixedAnswers; i++) { 
            var answer = $('input[id^="answer"][id$="X'+qID+(ansCount-fixedIndex)+'"]'); 
            var answerItem = $(answer).closest('.answer-item'); 
            var answersList = $(answer).closest('ul'); 
            $(answersList).append(answerItem); 
            fixedIndex--; 
        } 
 
        /* Handle "Other"*/ 
        if(otherFixed == true &amp;&amp; $('#question'+qID+' input[type="text"]').length > 0) { 
            var otherAnswer = $('#question'+qID+' input[type="text"]'); 
            var otherAnswerItem = $(otherAnswer ).closest('.answer-item'); 
            var otherAnswersList = $(otherAnswer ).closest('ul'); 
            $(otherAnswersList).append(otherAnswerItem); 
        } 
 
 
        /* Maximun answers*/ 
        var maxAnswers = 3; 
 
        /* Identify this question*/ 
        var thisQuestion = $('#question{QID}'); 
 
        function checkMax() { 
        $('input[type="checkbox"]', thisQuestion).prop('disabled', false); 
        if($("input:checked", thisQuestion).length >= maxAnswers) { 
        $('input[type="checkbox"]', thisQuestion).not(':checked').prop('disabled', true); 
        } 
        } 
 
        /* Initial checkbox states*/ 
        checkMax(); 
 
        /* Listener on the checkboxes*/ 
        $('input[type="checkbox"]', thisQuestion).on('change', function(e) { 
        checkMax(); 
        }); 
 
        /* Remove any "disabled" properties before submitting*/ 
        $('#limesurvey').on('submit', function(e) { 
        $('input[type="checkbox"]', thisQuestion).prop('disabled', false); 
        }); 
 
}); 
</script>
 
Last edit: 1 year 9 months ago by philemmons.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose