JQuery: keyup (): update div with content from text area ... line breaks?

I posted a working version here: http://jsfiddle.net/JV2qW/2/

I have a text box that updates (on keyup() ) a div with the text that is being entered. Everything works as it should, except that line breaks are not recognized.

html:

 <p>enter text</p> <textarea id='text'></textarea> <div id='target'></div> 

and jquery:

 $('#text').keyup(function(){ var keyed = $(this).val(); $("#target").html(keyed); }); 

Any thoughts on how to pass \n to <br/> or <p> tags?

thank you very much.

+4
source share
3 answers

You can replace any newlines with <br/>

 $('#text').keyup(function() { var keyed = $(this).val().replace(/\n/g, '<br/>'); $("#target").html(keyed); }); 

You can see the MDC RegEx article if you want to replace other things.

https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

+8
source

http://jsfiddle.net/omnosis/8XL7n/

replace '\n' with '<br />'

+4
source

Why don't you just replace it with a key value and replace it?

Here you have an example - section Convert Cutting Results

http://lawrence.ecorp.net/inet/samples/regexp-format.php

0
source

All Articles