creating textNodes and newline characters

Discussion in 'Javascript' started by jackwootton@gmail.com, Jul 11, 2007.

  1. Guest

    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
    , Jul 11, 2007
    #1
    1. Advertising

  2. Guest

    On Jul 11, 12:39 pm, "" <>
    wrote:
    > 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");
    , Jul 11, 2007
    #2
    1. Advertising

  3. d d Guest

    wrote:
    >> I do the replacement with the JavaScript replace() method, so that
    >> myString now looks like this:
    >>
    >> blah <br /> blah <br /> blah <br /> blah'


    It should look like this:

    blah <br>blah <br>blah <br>blah

    ~dd
    d d, Jul 11, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krzysztof Fink-Finowicki via .NET 247

    Unicode and newline characters in XML text sond via Web Service

    Krzysztof Fink-Finowicki via .NET 247, Oct 17, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    131
    Krzysztof Fink-Finowicki via .NET 247
    Oct 17, 2004
  2. Krzysztof Fink-Finowicki via .NET 247

    Unicode and newline characters in XML text sond via Web Service

    Krzysztof Fink-Finowicki via .NET 247, Oct 31, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    195
    Dan Rogers
    Nov 15, 2004
  3. Krzysztof Fink-Finowicki via .NET 247

    Unicode and newline characters in XML text sond via Web Service

    Krzysztof Fink-Finowicki via .NET 247, Nov 17, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    174
    Krzysztof Fink-Finowicki via .NET 247
    Nov 17, 2004
  4. Daz
    Replies:
    6
    Views:
    102
  5. Daz

    Blank textNodes in the DOM

    Daz, Dec 27, 2006, in forum: Javascript
    Replies:
    10
    Views:
    160
Loading...

Share This Page