When creating elements via code, I have encountered an issue where modifying the innerHTML
property of an element breaks any references to other elements that a
HTML is represented internally by a DOM object structure. Kind of like a Tree class in traditional programming languages. If you set innerHTML, the previous nodes in the parent node are destroyed, the new innerHTML is parsed, and new objects are created. The references are no longer the same.
div
|-- a..
The div object above contains an Anchor object as a child. Now set a variable link1 as a reference to the address of this Anchor object. Then the .innerHTML is += "<br />"
, which means all of the nodes of div are removed, and recreated dynamically based on the parsed result of the new value of .innerHTML. Now the old reference is no longer valid because the Anchor tag was re-created as a new object instance.
few things here.
first of all. strings are immutable hence doing element.innerHTML += "<br>"
acts as a complete read and rewrite.
second, why that is bad:
aside from performance, mootools (and jquery, for that matter) assigns special unique sequential uids to all referenced elements. you reference an element by calling a selector on it or creating it etc.
then consider that SPECIFIC element with uid
say 5. the uid
is linked to a special object called Storage
that sits behind a closure (so its private). it has the uid
as key.
element storage then works on a element.store("key", value")
and element.retrieve("key")
and finally, why that matters: events
are stored into element storage (eg, Storage[5]['events']) - do element.retrieve("events") and explore that in fireBug if you're curious.
when you rewrite the innerHTML the old element stops existing. it is then recreated but the event handler AND the reference to the function that you bound earlier will no longer work as it will now get a NEW uid
.
that's about it, hope it makes sense.
to add a br just do new Element("br").inject(element)
instead or create a templated fragment for the lot (fastest) and add in 1 big chunk, adding events after.