How to Fix Duplicate Element IDs for Accessibility

Duplicate element IDs happen when two or more elements on the same page share an id value. This is often caused by repeated templates, cloned carousel slides, or a menu that is rendered twice for mobile and desktop. By the end of this guide you will have found every duplicate, given each element a unique id, updated every reference that points to it, and re-tested the page.

By Updated ⏱ 20-40 minutes🎯 Beginner● Low impactAccessibility
Illustration: Unique element IDs fix guide
ACCESSIBILITY · FIX GUIDE Unique element IDs IMPACTEFFORT TIME20-40 minutes LEVELBeginner

Why it matters

Browsers and assistive technologies expect id values to be unique. When aria-labelledby, aria-describedby, aria-controls or <label for> points to an id that appears twice, screen readers use the first match. Users can then hear the wrong label, miss an error message, or operate the wrong control.

Duplicate ids on focusable elements can also break skip links, in-page anchors and JavaScript that relies on getElementById. The direct SEO impact is small, but these are cheap bugs that cause real usability failures and drag down your accessibility score.

✕Before<label for="email">Email</label>appears in both the header and footernewsletter forms, and both inputs use✓AfterThe header form usesid="newsletter-email-header" and thefooter form uses

What you need

Access to edit your theme, templates or component code
Chrome, Edge or Firefox with DevTools
The URL of the page flagged by the audit
Optionally, the axe DevTools browser extension

Step-by-step fix

1

List every duplicated id on the page

Open the flagged page and open DevTools with F12, then go to the Console tab. Paste the snippet below and press Enter. It prints each id that appears more than once, along with the number of copies and the matching elements.

Click any element in the output to jump to it in the Elements panel.

javascript
const m={};document.querySelectorAll('[id]').forEach(e=>{(m[e.id]=m[e.id]||[]).push(e)});Object.entries(m).filter(([,v])=>v.length>1).forEach(([id,v])=>console.log('#'+id,'x'+v.length,v));
Pro tip: Run the snippet after the page has fully loaded and after opening menus or modals, because many duplicates are injected by JavaScript.
2

Prioritise ids used by ARIA, labels and focus

Fix the most harmful duplicates first. These are ids referenced by aria-labelledby, aria-describedby, aria-controls, aria-owns, aria-activedescendant or <label for>, and ids on focusable elements such as links, buttons and inputs. Run the snippet below to see which duplicated ids are actually referenced.

javascript
const dup=new Set([...document.querySelectorAll('[id]')].map(e=>e.id).filter((id,i,a)=>a.indexOf(id)!==i));document.querySelectorAll('[aria-labelledby],[aria-describedby],[aria-controls],[aria-owns],[aria-activedescendant],label[for]').forEach(el=>{['aria-labelledby','aria-describedby','aria-controls','aria-owns','aria-activedescendant','for'].forEach(a=>{(el.getAttribute(a)||'').split(/\s+/).forEach(id=>{if(dup.has(id))console.log(a,'->',id,el)})})});
3

Find the template or component that repeats

Most duplicates come from a handful of sources:

  • a component rendered more than once, such as a newsletter form, product card or accordion
  • a navigation menu output twice, once for desktop and once for mobile
  • carousel plugins that clone slides
  • embed codes pasted twice

In the Elements panel, look at the parent markup or CSS classes of each duplicate to identify the template file, block or widget that produced it.

Pro tip: If one id appears as many times as you have products or posts, the id is hard-coded inside a loop.
4

Generate a unique id for each instance

Replace hard-coded ids with ones built from something that differs per instance, such as a loop index, a post or product ID, a section ID, or a framework helper. Keep a readable prefix so CSS and debugging stay easy. If an id is never referenced by anything, you can simply delete it.

html
<!-- Before (inside a product loop) -->
<button id="add-to-cart" aria-describedby="price">Add to cart</button>
<span id="price">$20</span>

<!-- After -->
<button id="add-to-cart-1042" aria-describedby="price-1042">Add to cart</button>
<span id="price-1042">$20</span>
Pro tip: In React use the `useId()` hook, and in Vue 3.5+ use `useId()`, so ids stay stable between server and client rendering.
5

Update every reference to the old id

Search your codebase for the old id value and update everything that points to it, or it will break. Check each of these:

  • for attributes on labels
  • aria-* attributes
  • in-page links such as href="#price"
  • CSS selectors such as #price
  • JavaScript calls such as getElementById('price') or querySelector('#price')

Where styling or scripts target many instances, switch to a class or a data- attribute instead of an id.

javascript
/* Before */
#add-to-cart { background:#0a5; }
document.getElementById('add-to-cart').addEventListener('click', add);

/* After */
.add-to-cart { background:#0a5; }
document.querySelectorAll('.add-to-cart').forEach(b => b.addEventListener('click', add));
6

Handle duplicated menus, modals and clones

If a menu is rendered twice, either output it once and restyle it with CSS media queries, or give the mobile copy a suffix such as -mobile on every id. For carousels that clone slides, remove ids from the slide markup or switch on a loop mode that does not clone them. For third-party widgets you cannot edit, embed each one only once per page.

html
<nav id="main-nav" aria-label="Main">...</nav>
<nav id="main-nav-mobile" aria-label="Main (mobile)">...</nav>
Pro tip: Removing an `id` that nothing references is the fastest fix and has no side effects.
7

Re-test the page

Clear any page or CDN cache and reload the page. Run the console snippet from step 1 again and confirm it prints nothing. Then run axe DevTools or re-run the audit.

Also test a few labels and controls you changed with a screen reader or the Accessibility pane in DevTools, to confirm each control now has the correct name and description.

Pro tip: Check one example of each page template, such as home, category, product and article, because duplicates usually live in shared templates.

Platform-specific notes

WordPress

In PHP templates, use `wp_unique_id( 'newsletter-' )` to generate a unique id for each render. If the duplicates come from a block or a page builder (Elementor, Divi), check the Advanced > CSS ID field for each widget, and clear any id copied when the widget was duplicated. Clear your caching plugin before re-testing.

Shopify

In Liquid, append a per-instance value to ids, for example `id="price-{{ section.id }}-{{ product.id }}"` or `{{ forloop.index }}` inside loops. Edit these under Online Store > Themes > ... > Edit code, in the relevant `sections/` or `snippets/` file. Duplicate theme sections often reuse the same hard-coded id.

Webflow

Custom ids are set in the Element Settings panel (the gear icon) under ID. Components (formerly Symbols) used more than once repeat any id set inside them, so remove the id or move it outside the component. Publish the site before re-testing.

Wix

You have little control over generated markup. Focus on custom code: in Settings > Custom Code and in Embed HTML elements, make sure each embed's ids are unique and that the same embed is not added twice on one page.

Next.js / React

Do not hard-code ids in reusable components. Call `const id = useId()` and build related ids from it, for example `${id}-label`, so they match between server and client rendering. Avoid `Math.random()`, which causes hydration mismatches.

Check that it worked

✅ How to verify

  • Run the console snippet from step 1 and confirm it prints no duplicate ids.
  • Run the axe DevTools extension and confirm there are no duplicate-id issues.
  • Re-run the audit on the same URL and confirm the Unique element IDs check passes.
  • In the DevTools Accessibility pane, inspect the edited inputs and buttons to confirm each one's Name and Description come from the intended element.

⚠️ Common mistakes

  • Renaming the id but leaving the old value in `for`, `aria-labelledby`, `href="#..."`, CSS or JavaScript, which silently breaks labels and behaviour.
  • Fixing only the page that was flagged instead of the shared template, so the duplicate returns on every other page that uses it.
  • Generating random ids on each render in a server-rendered app, which breaks hydration and changes anchor links.
  • Testing only the initial HTML and missing duplicates added later by menus, modals, carousels or chat widgets.

Is this still an issue on your site? Run a free audit — ZORO checks this and 294 other SEO, AI-visibility, accessibility and security signals, with fixes for your platform.

Check my site free

FAQ

Do duplicate IDs affect SEO?

Not directly. Search engines do not rank pages lower because of duplicate ids. The real cost is broken accessibility, unreliable in-page anchor links and fragile JavaScript, all of which can hurt user experience.

Can I use the same ID on different pages?

Yes. Ids only need to be unique within a single page (document). Using id="main" on every page of example.com is fine, as long as it appears once per page.

Why is my ID duplicated when it only appears once in my template?

The template is probably rendered more than once. Common causes are a component used twice, a menu output for both mobile and desktop, a carousel cloning slides, or a script injecting the same markup again. Inspect the live DOM rather than the source file to see where the copies come from.

Should I use classes instead of IDs?

Use classes for styling and for JavaScript that targets many elements. Keep ids for things that must be uniquely referenced, such as label targets, ARIA relationships, skip links and in-page anchors.