admin管理员组

文章数量:1303450

I'm trying to add a line break using inner HTML. My code so far is as follows:

document.getElementById("demo").innerHTML += "hello";
document.getElementById("demo").innerHTML += linebreakhere;

I've been searching, and is there a way to add like a <b> like the above?

I'm trying to add a line break using inner HTML. My code so far is as follows:

document.getElementById("demo").innerHTML += "hello";
document.getElementById("demo").innerHTML += linebreakhere;

I've been searching, and is there a way to add like a <b> like the above?

Share Improve this question edited Feb 6, 2023 at 2:58 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jun 16, 2016 at 2:49 ollielouisollielouis 91 gold badge1 silver badge3 bronze badges 3
  • 1 Either pass <br/> in string or add CSS block property to element so that element goes to next line – Shivani Commented Jun 16, 2016 at 2:53
  • 1 That code is inefficient. Re-querying the DOM for getting the same element and modifying innerHTML , the worst way of appending elements. – Ram Commented Jun 16, 2016 at 2:57
  • Re "<b>": Do you mean "<br>"? – Peter Mortensen Commented Feb 6, 2023 at 3:00
Add a ment  | 

2 Answers 2

Reset to default 7

You do this:

document.getElementById("demo").innerHTML += "hello";
document.getElementById("demo").innerHTML += "<br>";

A few ways you can achieve the line break using innerHtml

Example 1:

document.getElementById("demo").innerHTML = "Hello <br/> World";
<div id="demo"></div>

Example 2:

document.getElementById("demo").innerHTML = "<pre>" + "Hello" + "\n" + "World"+ "</pre>";
<div id="demo"></div>

本文标签: innerhtmlHow do I add a line break to a 39div39 using inner HTML in JavaScriptStack Overflow