简体   繁体   中英

Is .value preventing line breaks in the textarea?

How do I get line breaks in my template to show in the textarea?

I am trying to add a line break in each template after the , and can't use </ br> either I think due to using .value .

I tried JavaScript: How to add line breaks to an HTML textarea? and javascript | save textarea value with line breaks as well as a suggestion to use style="white-space:pre-wrap" .

 var template_Birthday = "Hey <NAME>, This is the Birthday Template."; var template_NewJob = "Hey <NAME>, This is the New Job Template."; var content = function() { var e = document.getElementById("template"); var strUser = e.options[e.selectedIndex].value; if (strUser === "template_NewJob") { messageBodyTextarea.value = template_NewJob; } else if (strUser === "template_Birthday") { messageBodyTextarea.value = template_Birthday; } }; 
 <textarea class="form-control" rows="7" name="message" id="messageBodyTextarea"></textarea> 

使用\\ n作为新行。

var template_NewJob = "Hey <NAME>,\nThis is the New Job Template.";

Since it looks like you're setting the value of a textArea , have you tried an escaped newline character:

messageBodyTextarea.value = "Hey <NAME>, \nThis is the Birthday Template.";

You only need to use <br /> if you're writing HTML to the DOM.

To add a line break to a textarea in HTML:

<textarea id="myTextarea">Line 1
Line 2
Line 3</textarea>

To add a line break to a textarea in JavaScript:

var ta = document.getElementById('myTextarea');
ta.value = 'Line1\nLine2\nLine3';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM