kknkh Posted October 18, 2012 Share Posted October 18, 2012 Hi. I want to add custom object in DOM.Then want to use custom object by DOM.Source is like this.[source]var div=document.createElement('div');div.setAttribute('obj', this);[/source]it works in IE.but it didn`t work in Chrome.Cause setAttribute add value as string in Chrome.I want to know then how to add object in DOM in chrome.otherwise i really want to know other way.Thanks to reading. Link to comment Share on other sites More sharing options...
Ingolme Posted October 18, 2012 Share Posted October 18, 2012 An attribute can only contain a string, because an attribute is supposed to be representable in printed HTML. If you want to add a Javascript object to the DOM of an element you'll have to add it as a property: var div = document.createElement("div");div.obj = this; Link to comment Share on other sites More sharing options...
thescientist Posted October 18, 2012 Share Posted October 18, 2012 worth a readhttp://stackoverflow.com/questions/9242009/storing-custom-data-in-dom-elements Link to comment Share on other sites More sharing options...
Ingolme Posted October 18, 2012 Share Posted October 18, 2012 OK, so Internet Explorer has a trashy garbage collector. But it looks like it's not harmful to do it that way anyways: For proper context, this only introduces a memory leak if the object in question is removed from the DOM and you expect it to be garbage collected. And, the memory leak is only of consequence if the data associated with the DOM element is huge or there are thousands of these that are created, then removed from the DOM. For an object that stays in the DOM for the lifetime of your page, there is no memory leak. I suppose the best thing is to have data stored in another object independent from the element. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now