javascript - js replace \n with and show -
javascript - js replace \n with <br> and show -
i have textarea input , show-div show input:
<textarea id="inputtext" style="height:100px"></textarea> <button id="show">show in div</button> <div id="showtext"> </div>
and js:
$(function(){ $('#show').on('click',function(){ var text = $.trim($('#inputtext').val()); text = text.replace(/\r?\n/g, "<br />").replace(/<br\s*[\/]?>/gi, "\n"); $('#showtext').text(text); }); });
what want is, if give: a
b
c
it should show in form. showing now: a b c
.
here fiddle: http://jsfiddle.net/5gkak/
what doing wrong?
you need alter fiddle match this: http://jsfiddle.net/5gkak/1/
$(function(){ $('#show').on('click',function(){ var text = $.trim($('#inputtext').val()); text = text.replace(/\r?\n/g, "<br />"); $('#showtext').html(text); }); });
what had replacing \n
<br />
, doing same in reverse <br />
getting replaced \n
. needed set html of #showtext
. check out difference of .text
, .html
in jquery docs.
there other options (these comment sections of question):
css-only (for formatting): http://jsfiddle.net/5gkak/2/ (this coolest solution imo) html-only (for formatting): http://jsfiddle.net/5gkak/3/ (this 1 doesn't work text!) javascript jquery html
Comments
Post a Comment