Jump to content

how to add custom object in DOM


kknkh

Recommended Posts

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...