Documentation and Books

Recent site activity

Web Development‎ > ‎JavaScript‎ > ‎

Is there an easy way to find all the double ID's in the DOM tree of a HTML page?

The following JavaScript function will display all the double ID's in the DOM tree of the HTML page using an alert message:

  function findDoubleId() {
var _tags = document.body.getElementsByTagName('*');
var _ids = {};
var _doubledIds = [];
for (var _index = 0; _index < _tags.length; _index++) {
var _tag = _tags [_index];
if (_tag.id) {
if (! _ids[_tag.id]) {
ids[_tag.id] = true;
} else {
_doubledIds.push(_tag.id);
}
}
}
alert('doubled ids: ' + _doubledIds);
}