Random result from CMS when the button is clicked

Hi guys!

How can I give out a random item from the cms when I click on a button?

Example:

I have a CMS with 100 values. I want the user to be shown a random one of those values when they click on the button. Then he can click again to see another one. And so until he gets bored or finds the right one.

Is this possible? Thanks!

hi @FlowKeeper you will need to create an array from items and use Math.floor Math.random and length of an array. The formula can looks like :
arr[Math.floor(Math.random() * arr.length]

SIMPLE EXAMPLE

1 Like

Hi @Stan I am trying to implement this same solution on a collection list.

How would I go about implementing this?

Do I paste the snippet you shared somewhere on the page? On a collection or in body? Where exactly?
arr[Math.floor(Math.random() * arr.length]

I would greatly appreciate your help.

I have a library of golf drills and I want to display only one drill on a home page - with a “randomize” button. The user would click it until they find the right drill.

Thanks

HI @Luka_Karaula and sorry for late response as I left WF platform long time ago. Anyway, You should have in your Home page the collection with your “drills” that are hidden. Then apply JS function that will randomly select one “drill” on page load to display it (display: block) and assign click event listener to button that will run this function again on click. Hope this will point you right direction. :love_you_gesture:

EDIT:
I have just created a simple example for you I have included condition to not repeat last displayed item as next.