Hi - I am using an image grid in my Hubspot webpage design but would like the order of the images to shuffle in some way as I will have large number of them. How can I achieve that easily?
This is easiest to do with a small JavaScript snippet added to the page.
Identify the CSS selector for your grid’s wrapper (e.g., .image-grid).
Upload a JS file in Design Manager, then include it on the specific page via the page editor: click Settings ➝ Advanced ➝ Footer HTML and add a <script src="...your file URL..."></script>.
Paste this code in your JS file and update .image-grid to match your grid’s selector:
document.addEventListener('DOMContentLoaded', function () {
const grid = document.querySelector('.image-grid'); // <-- change this to your grid selector
if (!grid) return;
const items = Array.from(grid.children);
// Fisher–Yates shuffle
for (let i = items.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[items[i], items[j]] = [items[j], items[i]];
}
// Re-render in new order
const frag = document.createDocumentFragment();
items.forEach(el => frag.appendChild(el));
grid.innerHTML = '';
grid.appendChild(frag);
});
HubSpot’s default image grid doesn’t have a built-in shuffle or randomize feature. To achieve random image order easily, you can: Use custom JavaScript on your page to shuffle the images dynamically when the page loads. Alternatively, manually reorder the images in HubSpot CMS each time (less practical for large numbers). Use a third-party plugin or widget that supports randomized image galleries and embed it in your page. If you want, I can help with a simple JavaScript snippet to shuffle your images automatically.
This is easiest to do with a small JavaScript snippet added to the page.
Identify the CSS selector for your grid’s wrapper (e.g., .image-grid).
Upload a JS file in Design Manager, then include it on the specific page via the page editor: click Settings ➝ Advanced ➝ Footer HTML and add a <script src="...your file URL..."></script>.
Paste this code in your JS file and update .image-grid to match your grid’s selector:
document.addEventListener('DOMContentLoaded', function () {
const grid = document.querySelector('.image-grid'); // <-- change this to your grid selector
if (!grid) return;
const items = Array.from(grid.children);
// Fisher–Yates shuffle
for (let i = items.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[items[i], items[j]] = [items[j], items[i]];
}
// Re-render in new order
const frag = document.createDocumentFragment();
items.forEach(el => frag.appendChild(el));
grid.innerHTML = '';
grid.appendChild(frag);
});