Show A Random CMS Item

Utility
CMS
JS
  • Give collection list a class of your-cms-list
  • Only one random child within this element will be shown on page load
<script>
document.addEventListener("DOMContentLoaded", () => {
  document.querySelectorAll(".your-cms-list").forEach(list => {
    const items = Array.from(list.children);
    const randomNumber = Math.floor(Math.random() * items.length);
    items.forEach((item, i) => {
      item.style.display = i === randomNumber ? "" : "none";
    });
  });
});
</script>
Last Updated: Dec 4, 2025
  • Give collection list a class of your-cms-list
  • Only one random child within this element will be shown on page load
<script>
document.addEventListener("DOMContentLoaded", () => {
  document.querySelectorAll(".your-cms-list").forEach(list => {
    const items = Array.from(list.children);
    const randomNumber = Math.floor(Math.random() * items.length);
    items.forEach((item, i) => {
      item.style.display = i === randomNumber ? "" : "none";
    });
  });
});
</script>