Welcome to the LimeSurvey Community Forum

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

Custom Random Java script for Question Type

More
2 weeks 6 days ago - 2 weeks 6 days ago #274100 by rajkumar_dms
Please help us help you and fill where relevant:
Your LimeSurvey version: [6.16]
Own server or LimeSurvey hosting:Cloud
Survey theme/template:
==================
Hello Team,

I need a custom java script for randomised option of the question ,whatever option we want to do not random it then we can fixed at their position for question Single & Multi choice question

If it is possible share the java for themes so we can called the script in the question source code
Last edit: 2 weeks 6 days ago by rajkumar_dms.

Please Log in to join the conversation.

More
1 week 6 days ago #274152 by rajkumar_dms
Hi,

Anyone look into it and share the solution for the same

Please Log in to join the conversation.

More
1 week 3 days ago #274178 by Joffm
Hi,
1. Create a question of type "short text". Let's call it "QOrder". Hide it with css class "d-none".
With the following script you generate a random order with one element at a fixed position
Code:
<script type="text/javascript" charset="utf-8">
 
    function shuffle(array) {
        var currentIndex = array.length, temporaryValue, randomIndex;
        // While there remain elements to shuffle...
        while (0 !== currentIndex) {
            // Pick a remaining element...
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
            // And swap it with the current element.
            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
        }
        return array;
    }
 
$(document).on('ready pjax:scriptcomplete',function(){
    // Define the code that has to be fixed
    var fixedCode= 7;
    // Fill the array with all codes, here Numbers fom 1 to 10
    var arr = [];
    for (var i = 1; i < 11; i++) {
        arr.push(i);
    }
    arr = shuffle(arr);
 
    // Find the fixed code
    const index = arr.indexOf(fixedCode);
    if (index > -1) { // only splice array when item is found
        arr.splice(index, 1); // remove the element
    }
 
    // Insert the fixed code at his position
    arr.splice(fixedCode-1, 0, fixedCode);
 
    $('#question{QID} input[type="text"]').val(arr);
});
</script>

Now in your single resp. multiple questions enter this script
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
 
    //Identify this question
    var thisQuestion = $('#question{QID}');
    var thisAnswerList = $('li.answer-item:eq(0)', thisQuestion).parent();
 
    // Retrieve the answer codes from the "QOrder" question
    var answerCodes = '{QOrder}'.split(',');
 
    // Loop through the answer codes
    $.each(answerCodes, function(i, val) {
      // Move the answer item
      $(thisAnswerList).append($('li.answer-item[id$="X{QID}'+val+'"]', thisQuestion));
    });
});
</script>




Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: rajkumar_dms

Please Log in to join the conversation.

More
1 week 3 days ago #274179 by rajkumar_dms
Thanks

Here i want some modification fixedCode may be 1 digit or more than 1 digit ,can share the java script for the same

Please Log in to join the conversation.

More
1 week 3 days ago #274184 by Joffm

fixedCode may be 1 digit or more than 1 digit

You can use whatever you want
text
 

Beware: Here you have to hardcode the position
arr.splice(fixedCode-1, 0, fixedCode);
as fixedcode is a text

numbers with more than one digit.
 


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

More
1 week 3 days ago #274185 by rajkumar_dms
if i want to fixed the more than 1 code means code 7,9,5 will fixed at their position

Please Log in to join the conversation.

More
1 week 3 days ago #274193 by Joffm
You see how it works.
From // Find the fixed code.
Just repeat the "remove - insert" as many times as there are fixed codes.
fixedCode1=5; fixedCode2=7;fixedCode3=9;
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

More
1 week 2 days ago #274197 by Joffm
Best practice:
First remove all fixedCodes, then insert starting with the smallest value

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

More
1 week 2 days ago #274198 by Joffm
Another, maybe easier way is to NOT include the fixed codes in the initial array.
So you don't have to remove them.
Only enter one by one.

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

More
1 week 1 day ago #274199 by Joffm
Here a solution a bit more elegant
With functions to remove and add the fixedCodes in an array.
Code:
<script type="text/javascript" charset="utf-8">
 
    function shuffle(array) {
        var currentIndex = array.length, temporaryValue, randomIndex;
        // While there remain elements to shuffle...
        while (0 !== currentIndex) {
            // Pick a remaining element...
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
            // And swap it with the current element.
            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
        }
        return array;
    }
 
$(document).on('ready pjax:scriptcomplete',function(){
 
    function removeCode(value) {
        var index = arr.indexOf(value);
        if (index > -1) { // only splice array when item is found
            arr.splice(index, 1); // 2nd parameter means remove one item only
        }
    }
 
    function addCode(value) {
        arr.splice(value-1, 0, value);
    }
 
    var fixedArray= [7,13,3,8];
    // Order of codes doesn't matter; in the next line it is sorted
    fixedArray.sort(function(a, b) { return a - b } );
 
    // Fill the array; here codes from 1 to 30
    var arr = [];
    for (var i = 1; i < 31; i++) {
        arr.push(i);
    }
 
    // First remove the codes that are to be fixed
    fixedArray.forEach(removeCode);
    // Shuffle the array
    arr = shuffle(arr);
    // and insert the fixed codes at their place
    fixedArray.forEach(addCode);
 
    $('#question{QID} input[type="text"]').val(arr);
});
</script>

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose