← All examples

JavaScript Navigation etsy.com

Etsy: keyboard arrows for photo gallery

Lets you flip through product photos with the left and right arrow keys, no mouse needed.

Code to copy

// Browse the listing photo gallery with the left/right arrow keys
function thumbs() {
  return Array.from(document.querySelectorAll(
    '[data-component="listing-page-image-carousel"] '
    + 'button, .listing-page-image-carousel-component li button'));
}
document.addEventListener('keydown', function (e) {
  if (e.target.matches('input, textarea')) { return; }
  const list = thumbs();
  if (!list.length) { return; }
  let idx = list.findIndex(function (b) {
    return b.getAttribute('aria-current') === 'true'
      || b.classList.contains('is-selected');
  });
  if (e.key === 'ArrowRight') { (list[idx + 1] || list[0]).click(); }
  if (e.key === 'ArrowLeft') {
    (list[idx - 1] || list[list.length - 1]).click();
  }
});

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. Paste the code into the rule's JavaScript panel and save — it runs on every page visit.

Rate this example

No ratings yet — be the first.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases