Home > Software design >  Javacript [ document.write ("___"); ] Shortcut to not start each line with document.write
Javacript [ document.write ("___"); ] Shortcut to not start each line with document.write

Time:06-05

In this code I want to insert some HTML and normal text in JavaScript and don't want it to always enter document.write(" in starting of each line like:

document.write("some text
some text
some text");

Instead of this:

document.write("Hello World!");
document.write("Have a nice day!");
document.write("<br>");
document.write("<p>some random paragraph");

document.write("Hello World!");
document.write("Have a nice day!");
document.write("<br>");
document.write("<p>some random paragraph");

CodePudding user response:

⚠Warning: Don't use document.write


Instead use document.body.innerHTML = '';

document.body.innerHTML = `
your text
<br>
your text
`;

CodePudding user response:

  1. Don't use document.write:

⚠ Warning: Use of the document.write() method is strongly discouraged.

  1. Use a template/string to create the HTML, and then assign it to the innerHTML of the document body (or whatever element you want).

document.body.innerHTML = `
  Hello World!<br />
  Have a nice day!<br />
  <p>some random paragraph</p>
`;

  • Related