Shuffle on click

Hello all,

I am building a section on my site that shuffles a text block every time you refresh using the great ->shuffle() property.
I was wondering if there is a trivial way to achieve that same effect on clicking a button, so it will then shuffle this block without needed to refresh.

I understand this is more a question not specifically related with Kirby, but I would appreciate any help if someone has ever build something similar.

Thanks in advance!

Two options:

  • do it purely client side with JS
  • do it with an AJAX command to the server

Thanks! Any idea how to approach this doing it purely with Jquery?

What do you actually currently shuffle? Could you post your code please?

Assuming you have some div or other elements inside a container, something like this should do the job:

Thank you. This is my snippet:

<?php $randomText = $site->text()->toStructure()->shuffle()->first() ?>
<?php echo $randomText->block()->kt() ?>

Oh, ok, you are just getting a single element.

I just thought I was blind!

Anyway, so in this case the code I posted above won’t work and you would have to use Ajax to request a new element from the server.

I originally thought you wanted to shuffle the order of elements.