Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show duplicate IDs in chrome dev console

Is there a way to get the chrome dev console to warn you about duplicate IDs on a page? I can't tell you how many times I've been stumped by an issue only to finally realize that 2 elements are sharing the same id and messing up the code. Does anyone have any other suggestions on how to catch things like this as well that chrome doesn't seem to warn about by default?

like image 425
AdamMasters Avatar asked Jun 07 '26 14:06

AdamMasters


1 Answers

Running this in devtools will print them out

const dupes = Object.entries(
    [...document.querySelectorAll("[id]")]
      .map((x) => x.id) /* get all ids */
      .reduce(
        (acc, id) => ({ ...acc, [id]: (acc[id] || 0) + 1 }),
        {}
      ) /*count them*/
  ).filter(([_key, val]) => val > 1); /* find the ones repeating more than once */
  
console.warn(dupes);
like image 179
beta Joe Avatar answered Jun 10 '26 03:06

beta Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!