vu******@gmail. com wrote:
Please, explain an interesting phenomenon, if you can. I have an array
of references to an HTML element:
<div id='container'>
<div id='someId1'></div>
<div id='someId2'></div>
</div>
....
myArray["someId1"] = document.getEle mentById("someI d1");
myArray["someId2"] = document.getEle mentById("someI d2");
If I repaint these HTML elements like this:
document.getEle mentById("").in nerHTML="<div id='someId1'><d iv
id='someId2'></div>"
What is the empty string parameter to getElementById( ) supposed to do?
with the same IDs, the reference to my objects in myArray is lost but
the element seems to exist, not on the webpage, though.
Where did you get the term 'repaint' from? You aren't repainting those div
elements, you are replacing them with completely different div elements.
The array will still reference the original elements, but they are no
longer on the page.
Do I need to build a new reference array when repainting the HTML
elements?
Yes, or better yet use DOM methods to manipulate the existing elements
instead of replacing them. For example, you could empty the existing div
elements like this:
function emptyNode(node) {
while (node.firstChil d) {
node.removeChil d(node.firstChi ld);
}
}
emptyNode(myArr ay["someId1"]);
emptyNode(myArr ay["someId2"]);
Where are the old elements which seem to still exist?
(myArray["someId2"]=="[object]")
The old elements will exist as long as you still reference them but are no
longer used in the displayed page. You could put them back into the DOM if
you wanted.