How to Create a New Line Element in JavaScript?

Estimated read time 1 min read

In JavaScript, you can create a new line element by using the document.createElement method and setting the innerHTML property of the element to include a line break character, \n. Here’s an example:

let newLine = document.createElement("p");
newLine.innerHTML = "This is a new line.";

In this example, we first create a new p element using the document.createElement method. Then, we set the innerHTML property of the element to a string that contains the text we want to display on the new line. Finally, we append the element to the body of the HTML document using the appendChild method.

Note that the line break character \n is not interpreted in HTML, so the line break will not be visible in the final output. If you want to create a visible line break, you can use a <br> element or add a CSS margin or padding property to the element.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply