1

I am working on trying to get a way for whatever is in a textarea to move over to a div on a button click. Some of the key features that i am looking for is that i want linebreaks to stay and that if a user inputs any kind of html tags, it will cross over shown the same way.

So something like this that is entered:

<body>
    <p>blah blah blah</p>
    <p>more text</p>
</body>

will still look the same in the div area.

I have tried a couple of things, but most recently I have tried this:

var value = $('#selector-input textarea').val().replace(/\n/g, '<br/>');
$('#selector-canvas #canvas').text(value);

Does anyone have any ideas? Any help would be much appreciated. Thanks!

scapegoat17
  • 5,509
  • 14
  • 55
  • 90

1 Answers1

5

If you want to preserve the tags you have to replace the angle brackets first, followed by replacing the newline characters - http://jsfiddle.net/jayblanchard/2Kg9F/

var value = $('textarea').val()
    .replace(/</g, '&lt;')
    .replace(/>/g,'&gt;')
    .replace(/\n/g, '<br/>');
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119