-1

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>
Community
  • 1
  • 1
anonameuser22
  • 108
  • 12

3 Answers3

2

Use \n for new line.

var template_NewJob = "Hey <NAME>,\nThis is the New Job Template.";
Sean Wessell
  • 3,490
  • 1
  • 12
  • 20
1

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

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';
Leon Adler
  • 2,993
  • 1
  • 29
  • 42