Click to See Complete Forum and Search --> : Variable - parsing text with formatting??


IT Spider
11-05-2003, 05:02 AM
I am trying to update paragraphs from an array of optional text - how do I get any formatting to be included?

\n is seen by the script as it is not printed, but it is not 'linefeeding either!?

function swapPara(theText)
{
var displayedText = document.getElementById("text");
do {
displayedText.removeChild(displayedText.childNodes[0]);
}
while (displayedText.hasChildNodes());
document.getElementById("text").appendChild(document.createTextNode(theText));
}

<a href="swapText('New \n Paragraph')>Change Paragraph</a>
<p id="text">Default<br>Text</p>

Thanks again

Neil

Fang
11-05-2003, 06:30 AM
function swapPara(theText) {
// clear old contents
var displayedText = document.getElementById("text");
do {
displayedText.removeChild(displayedText.childNodes[0]);
}
while (displayedText.hasChildNodes());
// insert new contents
var n=theText.split('\n');
displayedText.appendChild(document.createTextNode(n[0]));
for(var i=1; i<n.length; i++) {
displayedText.appendChild(document.createElement("br"));
displayedText.appendChild(document.createTextNode(n[i]));
}
}

IT Spider
11-05-2003, 07:27 AM
Thanks again matey

I thought it was going to be something simple but just couldn't figure it out - glad to see I wasn't being so daft!

Are there any decent books you can recommend that have a Glossary of all these Javascript/DOM functions in (and explained!)?

Cheers

Neil
IT Spider

Fang
11-05-2003, 08:28 AM
I just use the web. The Gecko DOM Reference (http://mozilla.org/docs/dom/domref/) and searches for relevent examples.