Welcome to the LimeSurvey Community Forum

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

JS to randomize characters in a string.

  • maxersloan
  • maxersloan's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 7 months ago #244001 by maxersloan
JS to randomize characters in a string. was created by maxersloan
Please help us help you and fill where relevant:
Your LimeSurvey version: Version 2.6.7-lts Build 171212 (please don't laugh!)
Own server or LimeSurvey hosting: Own
Survey theme/template: any
==================
Hello, Previously Joffm provided a great solution for me in order to randomly choose brands to rate in a survey. I'm revisiting this with some alterations (actually to make it simpler) and I can't seem to figure it out. Below is the link to the original post from a few years ago with Joffm's code. Now I'm just using eqM1 to create the character string, then eqM3 to randomize, then eqM4 to pick the first from the random list. My problem is I cannot get eqM3 to randomize the character list. The data populates in eqM3 but does not randomize. Attached is the LSS file. Why is this not working?

forums.limesurvey.org/index.php/forum/ca...acking-study?start=0

Below is the eqM3 JS from Joffm
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(){
// Fill the array
      var arr =  $('#question{QID} input[type="text"]').val();
      var nArr = arr.split(', ');
      arr = shuffle(nArr);
      anumbers = arr.slice(0,1);
      $('#question{QID} input[type="text"]').val(anumbers);
      $('#question{QID}').hide();
$('#ls-button-submit').trigger('click');      
   });
</script>

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 7 months ago #244003 by holch
Replied by holch on topic JS to randomize characters in a string.

Version 2.6.7-lts Build 171212 (please don't laugh!)


I hadn't seen this in your previous question.

This isn't a cause for laughter. It makes me want to cry. 5,5 years...

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 7 months ago #244012 by Joffm
Replied by Joffm on topic JS to randomize characters in a string.
Why it doesn't work?
Because it isn't compatible to your version

​​​​​​
 

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

Please Log in to join the conversation.

  • maxersloan
  • maxersloan's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 7 months ago #244020 by maxersloan
Replied by maxersloan on topic JS to randomize characters in a string.
It was compatible 3 years ago. Why would it not be compatible now?

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 7 months ago #244025 by Joffm
Replied by Joffm on topic JS to randomize characters in a string.
There are IDs that don't exist in your version

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

Please Log in to join the conversation.

  • maxersloan
  • maxersloan's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 7 months ago #244034 by maxersloan
Replied by maxersloan on topic JS to randomize characters in a string.
I also have a version of 5.4.13 is there a way to do this?

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose