Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get hash string / guid from a jQuery object

I would like to have a hashtable of jQuery objects and needs some kind of unique identifier for them.

The DOM representation is just

<tr>
    ...
</tr>

And I would like to add my jQuery <tr> objects to a hashtable

hash[$(trObject).guid()] = $(trObject);

How can I uniquely identify a particular jQuery object without extending jQuery?

I know I could write a method

(function() {
    var guid = 0;
    $.fn.guid = function() {
         var node = this[0];
         if (node.guid === undefined) {
              node.guid = guid++;
         }
         return node.guid;
    };
}());

To do this for me, but I would prefer to know if there is some kind of native standard way to get a string/int hashcode from a jQuery object.

P.S. I don't expect to hit the 32bit integer limit.

like image 805
Raynos Avatar asked Nov 15 '22 01:11

Raynos


1 Answers

No native standard way which I am aware of but there are jquery plugins for this
eg. http://plugins.jquery.com/project/GUID_Helper

UPDATE:
The guid number used by jquery is stored in jquery.guid
Moreover, jquery is not using any native standard method for this. It is also following an approach similar to yours.

like image 74
sv_in Avatar answered Dec 05 '22 22:12

sv_in