0

I'm trying to populate the value of a textarea in my html with javascript. My script below basically does that, however I'm trying to figure out how to insert a line break in my value (between 'date' and 'text').

Basically, as written, my script gives this result...

On Dec. 25 you replied: Merry Christmas!

However, I would like it to read as...

On Dec. 25 you replied:
Merry Christmas!

<script>
function myFunction() {
  document.getElementById("myTextarea").value = `On ${date} you replied: ${text}`;
}
</script>
palaѕн
  • 72,112
  • 17
  • 116
  • 136

2 Answers2

1

Just add \n in the string to push it to a new line.

  <script>
    let date= "11/01/2020";
    let text= "Merry Christmas!";
    function myFunction() {
      document.getElementById("demo").value= `On ${date} you replied: \n${text}`;
    }
    </script>
Mr Khan
  • 2,139
  • 1
  • 7
  • 22
1

Simple add the new line escape sequence character: \n

<script>
function myFunction() {
  document.getElementById("myTextarea").value = `On ${date} you replied: \n${text}`;
}
</script>
Udo E.
  • 2,665
  • 2
  • 21
  • 33