fhaider
fhaider

Reputation: 165

Add new line in javascript while displaying HTML

Is there a way to add new lines in javascript while printing html? So that the printed html is indented.

document.getElementById("id").innerHTML = "<div class="1"><div class="2">hello</div></div>"

Instead I want to something like this:

document.getElementById("id").innerHTML = "
<div class="1">
    <div class="2">
        hello
    </div>
</div>"

Upvotes: 0

Views: 94

Answers (1)

Dmytro Shevchenko
Dmytro Shevchenko

Reputation: 34591

document.getElementById("id").innerHTML =
   ['<div class="1">',
    '    <div class="2">',
    '        hello',
    '    </div>',
    '</div>',
   ].join('\n');

Or just escape new lines:

document.getElementById("id").innerHTML =
"<div class="1"> \
    <div class="2"> \
        hello \
    </div> \
</div>";

Or the same with jQuery:

$("#id").html(
"<div class="1"> \
    <div class="2"> \
        hello \
    </div> \
</div>");

Upvotes: 1

Related Questions