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
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