Yearly Archives: 2021


Pure Javascript snippet to find duplicate DOM ids

Sometimes is necessary to find out duplicate element ids in DOM.
There is a pure javascript snippet to copy paste in you browser console,
which will inform you the number of duplicates and will enumerate them.

/* Find duplicate DOM ids | (c) 2021 - Petros Kyladitis */
let uid = Array() ;
let did = Array() ;
let e = document.querySelectorAll('*[id]') ;
for(i=0; i<e.length; i++){
  if(!uid.includes(e[i].id)){
    uid.push(e[i].id) ;
  }else{
    did.push(e[i].id) ;
  }
}

let msg = "There are " + (did.length>0 ? did.length : "NO") + " duplicate ids in DOM\n" ;
for(j=0; j<did.length; j++){
  msg += "\n" + did[j] ;
}

console.log(msg) ;