Outputting HTML into a textarea

Discussion in 'JavaScript' started by chuckd1356, Feb 12, 2008.

  1. #1
    So, I've just encountered something I've never seen before. This is what I did... kind of.
    
    document.write('<textarea>');
    document.write('...in here goes a whole bunch of html that I don't want to be formatted');
    document.write('</textarea>');
    
    Code (markup):
    But the problem is, the html contains '</textarea>' so it just ends the area, and the rest of the html is parsed and displayed in the browser (formatted).

    Assume I can't edit the html, cause it needs to be displayed exactly as it is.

    How can I put all this html into the textarea and have it be unformatted, and not prematurely end the textarea??

    Thanks in advance.
     
    chuckd1356, Feb 12, 2008 IP
  2. nhl4000

    nhl4000 Well-Known Member

    Messages:
    479
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Hey, it seems to work for me, this is the code I used.

    <script>
    document.write('<textarea>');
    document.write('<BR><TEST><img src="a.gif">');
    document.write('</textarea>');</script>
    Code (markup):
     
    nhl4000, Feb 12, 2008 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    var myTextVar = 'blah blah blah <textarea> <html> whatever I want';
    var ta = document.createElement('textarea');
    ta.appendChild(document.createTextNode(myTextVar));
    document.body.appendChild(ta);
    PHP:
    The trick is using createTextNode. It will not parse any html.
     
    MMJ, Feb 13, 2008 IP