Welcome to the LimeSurvey Community Forum

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

Can I select a random answer from a multiple choice question

  • elenas
  • elenas's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 3 months ago #210879 by elenas
Hi,
i have to randomly select one of the answers given in a previous multiple choice question.
I've found this script in another post but it didn't work in version 2.06+ (without "$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true);
        $('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');"  ,  last two commands  )

<script type="text/javascript" charset="utf-8">
 
    $(document).on('ready pjax:scriptcomplete',function(){
        // Identify this question
        var qID = {QID};
        var thisQuestion = $('#question'+qID);
 
        // A function to shuffle elements or contents of arrays
        (function($){
            $.fn.shuffle = function() {
                return this.each(function(){
                    var items = $(this).children();
                    return (items.length)
                        ? $(this).html($.shuffle(items))
                        : this;
                });
            }
 
            $.shuffle = function(arr) {
                for(
                    var j, x, i = arr.length; i;
                    j = parseInt(Math.random() * i),
                    x = arr, arr = arr[j], arr[j] = x
                );
                return arr;
 
            }
        })(jQuery);
 
        // Build an array of codes of all visible sub-questions
        var sqCodes = [];
        $('li.answer-item:not(.ls-irrelevant)', thisQuestion).each(function(i) {
            var thisCode = $(this).attr('id').split('X'+qID)[1];
            sqCodes.push(thisCode);
        });
 
        // Shuffle the sub-question codes array
        sqCodes = $.shuffle(sqCodes);
 
        // Check the first two sub-question codes
        $('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).prop('checked', true);
        $('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
        $('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true);
        $('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');




    });    
</script>

Thanks in advance
The topic has been locked.
More
3 years 3 months ago #210881 by jelo
You might create a demosurvey with just a few questions and attach the LSS-export here. Personally it's a pity that LimeSurvey never considered adding dynamic and static list support to use given answers as lists and do nice things via ExpressionScript with it.

Feature request: 11688: List building - static lists - dynamic lists
bugs.limesurvey.org/view.php?id=11688

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #210890 by Joffm

but it didn't work in version 2.06+

Does it mean, you work with 2.06?

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • elenas
  • elenas's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 3 months ago #210891 by elenas
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #210900 by holch

Does it mean, you work with 2.06?

yes

You shouldn't. ;-)

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose