Home / E-commerce and Business / Product Variant Picker

Product Variant Picker in JavaScript, Free with Live Demo

Free product variant picker in plain JavaScript. Choose colour and size, see the product image, price and stock change, sold out sizes greyed out, and a shareable URL for each variant.

Open live demoDownload HTML fileView code on GitHub
Product Variant Picker JavaScript project: a product page where colour and size change the picture, price and stock

Runs on: URLSearchParams and history.replaceState. Works in all modern browsers.

What is the Product Variant Picker?

A product page for a hoodie with five colours and six sizes. Picking a colour recolours the picture and shows which sizes are left.

Sizes that are sold out are crossed out, low stock gets a warning, bigger sizes cost a little more, and the address bar always holds your exact choice.

Good for

  • Clothing and shoe shops
  • Furniture with fabric and finish options
  • Phone cases and accessories
  • Any product with options

What this project does

How it works

  1. Stock per variantStock is kept per colour and per size. Changing colour redraws the sizes and disables any that are sold out.
  2. One picture, many coloursThe hoodie is an inline SVG, so changing colour just updates one fill. With photos, swap the image source instead.
  3. Shareable URLURLSearchParams writes the colour and size into the address bar with replaceState, and reads them back when the page opens.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

const params = new URLSearchParams(location.search);
let color = params.get("color") || "plum", size = params.get("size") || "";

function update() {
  sizes.forEach((s, i) => inputs[i].disabled = stock[color][i] === 0);
  hoodie.setAttribute("fill", colors[color]);
  history.replaceState(null, "", "?" + new URLSearchParams({ color, size }));
}

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

Why radio inputs for swatches?

They give keyboard support and one choice at a time for free, and screen readers announce the colour name.

How do I use real photos?

Keep an image list per colour and change the img src when the colour changes.

Why put the choice in the URL?

Customers can share or bookmark the exact product, and your ads can link straight to it.

More E-commerce and Business projects