By Creating Elements
To insert line breaks, you can create a <br> element and append it to the target element of the webpage. This method is particularly useful while dealing with more complex DOM manipulation scenarios or when you need to insert line breaks dynamically based on specific conditions.
Example: This illustrates how to add a line break and text to a div element using JavaScript.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="myElement">lineone</div>
<script>
let element = document.getElementById('myElement');
let br = document.createElement('br');
element.appendChild(br);
element.innerHTML += 'linetwo';
</script>
</body>
</html>
Output:
How to Make a Breakline in JavaScript for innerHTML?
In JavaScript, while working with HTML you will frequently need to add line breaks whether building a complex or simple web application. In this article, you will learn to insert line breaks in JavaScript’s innerHTML.
These are the following approaches:
Table of Content
- Using < br > Tag
- Using Template Literals
- By Creating Elements
- Using CSS
Contact Us