Yes, I understood that. That's why I said you can keep an unique ID somewhere else, because the one created by data() can't be used:
//create an unique ID var generateID = function(){ var uid = 0; return function(){ return uid++ }; } //save it somewhere $('img').data('id',id) or $('img').attr('id','img'+id) //or in a custom attribute Or instead, just make sure you are keeping at least one data pair per element, like an empty placeholder. And doh, now I realize that the code snippet is from jQuery itself :/ cheers, - ricardo On Dec 16, 7:35 pm, ricardoe <ricar...@gmail.com> wrote: > Hi Ricardo > > Yeah, its a DOM element to be more specific I'm working with a lot of > IMG elements. > I need to identify them uniquely, (because sometimes the image is > repeated, but I need to manage them as different objects), so when I > read the documentation for jQuery.data() I (maybe mis-)understand that > it will create a Unique id for an element, and its sort of true BUT > don't you dare to use removeData() for that element because if you > remove all the data attached with jQuery(elem).data('key','value') you > will get rid of the unique-id for the element too. Does that make > sense? > > On 16 dic, 10:33, Ricardo Tomasi <ricardob...@gmail.com> wrote: > > > You can keep your own unique identifier anywhere you want, for data() > > the identifier is the element itself. If you care to explain a bit > > more of what you're trying to achieve someone might come up with an > > useful suggestion. What did 'elem' refer to in your original script > > for example? A DOM element? > > > - ricardo > > > On Dec 16, 6:46 am, ricardoe <ricar...@gmail.com> wrote: > > > > Well, solved issue: > > > > Don't trust jQuery.data(element) if you're pretending to use gQuery > > > (element).data('only-data-element','something')!!!! > > > When you use gQuery(element).removeData('only-data-element') the > > > jQuery.cache for that element will be deleted. > > > This leads that when you do again jQuery.data(element) you WONT have > > > the expected ID, it will create a new one. > > > > Why?! I dunno. But I'll not file a ticket cause "most of people > > > doesn't use it that way"...... lame. > > > > On Dec 16, 1:39 am, ricardoe <ricar...@gmail.com> wrote: > > > > > Hi, > > > > > Does anyone knows why this: > > > > > var id = elem[ expando ]; > > > > // Compute a unique ID for the element > > > > if ( !id ) { > > > > id = elem[expando] = ++uuid; > > > > } > > > > > Could be being broken? I mean, on a test page I made the elem[expando] > > > > property is not being "saved". > > > > I'll explain, the first time I do jQuery.data(element) I get a number > > > > lets say: 10. > > > > Then if I do again the same (with the exactly same element) I get: 14. > > > > > I'm using this to identify elements so its not working as expected, > > > > anyone have theories about this?