Search code examples
javascriptpre

Is there a way to write multiple line Pre's in javascript


So I am trying to write a code where it has a pre tag and it turns into another value with multiple lines. Here is my code:

var pre = document.getElementById("text");
pre.innerHTML = "
Line 1
Line 2
Line 3
Line 4
"

When I do this it gives me error:
Uncaught SyntaxError: Invalid or unexpected token The line which gives me the error is

pre.innerHTML = "

Solution

  • Use Template Literals for Mulitline strings

    var pre = document.getElementById("text");
    pre.innerText = `
      Line 1
      Line 2
      Line 3
      Line 4
    `