Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing custom data in dom elements

Tags:

javascript

dom

Is this wrong? If so why?

var elm = document.getElementById("myElm");
elm.customValue = {attr1 : "test", attr2 : "test"};

I understand how to use the data attributes, but I don't want to muddy up my dom with all those attributes.

like image 254
Phillip Burch Avatar asked Feb 11 '12 16:02

Phillip Burch


Video Answer


1 Answers

This introduces a memory leak in some browsers because you bind a native C object (the DOM element) and a JS object together and some garbage collection algorithms cannot deal with this. IE is one of them.

Here is an MSDN article about IE memory leaks: http://msdn.microsoft.com/en-us/library/ie/bb250448(v=vs.85).aspx

like image 105
J. K. Avatar answered Oct 21 '22 21:10

J. K.