Click to See Complete Forum and Search --> : Frustrated by Nodes
IT Spider
11-03-2003, 09:46 AM
I am trying to replace a paragraph containing line breaks but cn't find the right code in order that the line breaks are ignored - I know it's somewhere in the DOM and node issues but how do you read/write to all nodes?
function swapCaption(theCaption,){
var displayedCaption = document.getElementById("caption");
displayedCaption.firstChild.nodeValue = theCaption;}
.
.
.
<p id="caption"> Text<br>More Text</p>
.
.
Thanks guys and girls
Neil
Your function will replace "Text" with theCaption.
To remove the contents of <p> and insert "New text"
var oC=document.getElementById("caption");
var contents= oC.childNodes;
for (var i=0; i<contents.length; i++) {
oC.removeChild(contents[i]);
}
document.getElementById("caption").appendChild( document.createTextNode("New text"));
IT Spider
11-04-2003, 05:40 AM
Dear Fang,
A big thank you as that certainly improves things...however for some unknown reason, after the third iteration I am getting the new text merely appending to the previous text and on the fourth - the first text is removed and the new text appended to the previously appeanded text!
There also appears to be some latent line breaks left behind that get cleared out with each iteration?
(I have replaced "New Text" with a variable text string parsed to the function)
Any ideas?
Thanks again
Neil
(PS: Can you recommend any good step through debuggers which may help me in future?)
function swapCaption(theCaption){
var oC=document.getElementById("caption");
do {
oC.removeChild(oC.childNodes[0]);
}
while(oC.hasChildNodes());
document.getElementById("caption").appendChild( document.createTextNode(theCaption));
}
I never use debuggers, only Mozilla's Javascript Console and alert()
IT Spider
11-05-2003, 03:32 AM
I bow to the Supreme Master - thanks - wish I had your brain (I think!)
Neil
IT Spider