Tutorial :Unique identifier for HTML elements


Besides of the ID, if you say, you want a unique identifier for an HTML element (let say a div).

I browsed the DOM for something (like a number or string) that is unique for each element; but the DOM is big and I failed to find that in the Internet.

Is there any property (in the DOM obviously) that is unique only to that element? (Other than the ID and also you don't specify it, but comes when the DOM is constructed)


As Pekka says, it would be easier if you would describe what you want to do. Until then here are two suggestions.

Unless you actually need to express the id as some kind of string you can save the normal DOM reference.

If you do need to express it as a string for some reason, then you'll need to assign a unique id yourself.

var getId = (function () {    var incrementingId = 0;    return function(element) {      if (!element.id) {        element.id = "id_" + incrementingId++;        // Possibly add a check if this ID really is unique      }      return element.id;    };  }());  


The only other identifier I can think of is the XPath of the element in the document.

For instance, the title link inside the heading of this very page has an XPath of


But like Pekka already said, it depends on what you want to do. And I dont think you can get the Xpath easily from the DOM in JavaScript, despite Xpath being available nowadays in JS engines.


There is the name attribute that can be addresses by document.getElementByName.

I don't think other unique identifiers exist - even though you could simulate one by setting a property (like title) to a unique value, and then query for that. But that is kludgy.

What exactly do you need this for? If you give more information about your situation, somebody will probably come up with a suggestion.


Internet explorer has a property "uniqueID" for every element. The problem is that the other browsers don't support it.


You can use a library or roll your own to create a unique identifier. JQuery has .data():

Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.


I just encountered the same situation, and while I was looking into some DOM elements in the Chrome dev tools inspector, I noticed that they all seem to have a property like jQuery11230892710877873282 assigned with a unique number. Obviously the number after 'jQuery' is different every time you load the page, my guess is that jQuery is generating this internally every time it tries to access or manipulate any DOM element. I played a little bit with it, and it looks like elements that are never accessed/manipulated by jQuery may not have this property, but the moment you do something like $(elem) the property will be there. So, since we're using both jQuery and lodash, I devised the following function to return a unique ID regardless of whether the element actually has a DOM id attribute.

_.reduce($(elem), function(result, value, key) { if(_.startsWith(key, 'jQuery')) return value; }, 0)  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »