| Article: |
Dynamic Content with DOM-2 (Part I of II) | |
| Subject: | The quick brown fox... | |
| Date: | 2005-04-27 12:42:27 | |
| From: | normalforce | |
|
I have noticed something interesting. When I click on the "call replaceSpan()" button the first time and have the fox jump ontop of the astounded zebra I don't get any Javascript errors. However, the next time I click on it, I get an uncaught exception error on the line that with the replaceChild method.
|
||
Showing messages 1 through 1 of 1.
-
The quick brown fox...
2005-04-27 13:16:48 normalforce [View]



The problem was, after changing the content the first time, the newSpan was never assigned a value of the old id. Thus when you run the function again,
var spanElm = document.getElementById("ex3Span");returns a null because ex3Span doesn't exist anymore. To get around that, just assign newSpan the old id, as so:
newSpan.id = spanElm.id;Now it works like a charm!