Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  175] [ 4]  / answers: 1 / hits: 38686  / 14 Years ago, wed, july 21, 2010, 12:00:00

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


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


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


More From » html

 Answers
23

Depending on the objective, 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;
};
}());

[#96157] Monday, July 19, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emileef

Total Points: 724
Total Questions: 108
Total Answers: 102

Location: Romania
Member since Sun, Dec 20, 2020
3 Years ago
;