Reputation: 2613
I want to save a reference to a specific object reflecting properties for the current dom element for some elements of the dom tree. Each element could have a different reference to another object or they could reference the same. Is it possible to use html5's data property therefore or is there another way to do that in plain javascript (not jquery).
Thanks for help!
Upvotes: 2
Views: 1905
Reputation: 4696
If you're simply looking to store static properties, then you can use the setAttribute
method on each dom element, like so
//Assume x is a dom node
x.setAttribute( 'data-myproperty' , JSON.stringify( your property object ) )
But if you're looking at things that aren't JSON encodable ( or rather cannot be represented as strings ), then you'll have to look at other ways.
The jQuery data method doesn't actually store anything on the dom element itself. It maintains an internal cache with all the data that is passed to it.
Upvotes: 3