The .val() of a textarea doesn't take new line

2019-01-09 03:46发布

The .val() property of an item in jQuery for a <textarea> doesn't seem to work with new lines. I need this function as the text-area is meant to recognize the enter key and display it as a preview, with the new line in tact.

However, what happens is shown in this fiddle: http://jsfiddle.net/GbjTy/1/. The text is displayed, but not in a new line.

How can I achieve the preview to include new lines, and I know it is possible, because it does this in the Stack Overflow Post Question preview.

Thanks.

P.S I have seen other links on SO relating to this, but they all say get the End User to use some code, which is not an ideal method for me. How can I achieve this without the End User writing any specific code, like here on SO Question Preview, where the Enter works as it should in the preview.

10条回答
疯言疯语
2楼-- · 2019-01-09 04:05

Your newlines output perfectly, but HTML ignores newlines: it needs <br>.

Use a simple newline-to-br function like so:

function nl2br_js(myString) {
var regX = /\n/gi ;

s = new String(myString);
s = s.replace(regX, "<br /> \n");
return s;
}

$( "#watched_textarea" ).keyup( function() {
       $( "#output_div" ).html( nl2br_js($( this ).val()) );
    }); 

jsfiddle here: http://jsfiddle.net/r5KPe/

Code from here: http://bytes.com/topic/javascript/answers/150396-replace-all-newlines-br-tags

查看更多
淡お忘
3楼-- · 2019-01-09 04:08

I had tried to set the textarea value with jquery in laravel blade template, but the code was breaking because the variable had new lines in it.

my code snippet was

$("#textarea_id").val("{{ $php_variable }}");

I solved my problem by changing my code snippet like below.

$("#textarea_id").val("{{ str_replace(array('\r', '\n', '\n\n'), '\n', $php_variable }}");

if any of you are facing this problem may take benifit from this.

查看更多
SAY GOODBYE
4楼-- · 2019-01-09 04:09
$( "#watched_textarea" ).keyup( function() {
   $( "#output_div" ).html( $( this ).val().replace(/[\r\n]/g, "<br />") );
});

You need to replace the new-line characters with <br> tags for HTML output.

Here is a demo: http://jsfiddle.net/GbjTy/3/

查看更多
该账号已被封号
5楼-- · 2019-01-09 04:11

The forced new lines in textareas are \ns, other HTML tags than textarea will ignore these. You have to use <br /> to force new lines at those elements.

I suggest you use JavaScript to fix that rather than CSS as you already rely on JavaScript.

$( "#watched_textarea" ).keyup( function() {
   $( "#output_div" ).html( $( this ).val().replace(/\n/g, '<br />') );
}); 

You can find the updated version here: http://jsfiddle.net/GbjTy/2/

查看更多
看我几分像从前
6楼-- · 2019-01-09 04:13

I have found a more convenient method using jquery.

samplecode

HTML

    <textarea></textarea>
    <div></div>
    <button>Encode</button>

JS

$('button').click(function() {

    var encoded = $('<label>').text($('textarea').val()).html();//the label element can be replaced by any element like div, span or else

    $('div').html(encoded.replace(/\n/g,'<br/>'))
});
查看更多
Root(大扎)
7楼-- · 2019-01-09 04:15

In the case that

  • you are NOT displaying the .val() of the textarea in another page (for example, submit form to Servlet and forward to another JSP).

  • using the .val() of the textarea as part of a URL encoding (e.g. a mailto: with body as textarea contents) within Javascript/JQuery

then, you need to URLEncode the textarea description as follows :

var mailText = $('#mailbody').val().replace(/(\r\n|\n|\r)/gm, '%0D%0A');
查看更多
登录 后发表回答