creating textNodes and newline characters

J

jackwootton

Hello,

I am given a string e.g.

var myString = 'blah blah blah blah';

The string myString, contains newline characters ("\n"). I need to
replace the newline characters with <br /> elements, so that when the
string is displayed in a document, the lines that are separated appear
separated.

I do the replacement with the JavaScript replace() method, so that
myString now looks like this:

blah <br /> blah <br /> blah <br /> blah'

I then create a new TextNode using:

document.createTextNode(myString);

However, the <br /> elements are displayed as string values, and are
not interpreted by the browser as <br /> elements.

to overcome this problem, I split the original string:

var myStringArray = myString.split("\n");

I then create a textNode for each item in the myStringArray array, and
a subsequent br element for each item in the myStringArray. I create
the br element using:

var brNode = document.createElement(br);

I then append the textNodes I create (from the array), and a brNode
between each textNode. With the aim that I would end up with:

textNode (blah)
brNode (br)
textNode (blah)
brNode (br)

However, the browser crashes / hangs when I attempt this. I think
this is because the operations are expensive and timely and cannot be
performed in a loop or used in this manner.

How can I solve my problem?

Thanks,

Jack
 
J

jackwootton

Hello,

I am given a string e.g.

var myString = 'blah blah blah blah';

The string myString, contains newline characters ("\n"). I need to
replace the newline characters with <br /> elements, so that when the
string is displayed in a document, the lines that are separated appear
separated.

I do the replacement with the JavaScript replace() method, so that
myString now looks like this:

blah <br /> blah <br /> blah <br /> blah'

I then create a new TextNode using:

document.createTextNode(myString);

However, the <br /> elements are displayed as string values, and are
not interpreted by the browser as <br /> elements.

to overcome this problem, I split the original string:

var myStringArray = myString.split("\n");

I then create a textNode for each item in the myStringArray array, and
a subsequent br element for each item in the myStringArray. I create
the br element using:

var brNode = document.createElement(br);

I then append the textNodes I create (from the array), and a brNode
between each textNode. With the aim that I would end up with:

textNode (blah)
brNode (br)
textNode (blah)
brNode (br)

However, the browser crashes / hangs when I attempt this. I think
this is because the operations are expensive and timely and cannot be
performed in a loop or used in this manner.

How can I solve my problem?

Thanks,

Jack

var brNode = document.createElement(br) is actually var brNode =
document.createElement("br");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top