Line Break in all div's html with JavaScript

Hi E's,
I try to get a solution for make like a simple custom beautify html.
In practice, every time the script found a div close (</div>), create a line break, like this:
<script>
var code_html = '<div id="all"><div class="one">hello, this is a test</div><div class="two">hello, test again</div><div class="one">hello, this is great</div></div>';
</script>

Open in new window

The objective is transform the variable code_html in this (create a line break for each </div>):
<script>
var code_html = '<div id="all"><div class="one">hello, this is a test</div>
<div class="two">hello, test again</div>
<div class="one">hello, this is great</div>
</div>';
</script>

Open in new window


How I can do that transformation i JavaScript, or in jQuery?

The best regards,
JC
LVL 4
Pedro ChagasWebmasterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
you want to create this in your code itself ?
Pedro ChagasWebmasterAuthor Commented:
Hi @Leakim,
Yes.
I create my own code editor for compose some articles in html, but every time I save the code comes like this:
<div id="David_Bowie_Psaablo_Picasso"><div class="row"><div class="col s1 m3 l3 xl3"></div><div class="col s10 m6 l6 xl6"><div class="video-container"><iframe width="853" height="480" title="David Bowie - Psaablo Picasso" src="https://www.youtube.com/embed/Gv9eWsKHazk?rel=0&showinfo=0" scrolling="no" allowfullscreen="allowfullscreen"></iframe></div></div><div class="col s1 m3 l3 xl3"></div></div></div>
I have above code inside a javascript variable, and I want organize/beautify my code.

Regards, JC
leakim971PluritechnicianCommented:
OK, so we just need to add a \n after each </div>, check here and confirm please : https://jsfiddle.net/6j7erx18/

$("body").append("<div>one</div><div>two</div><div>three</div>");

$("body").append("\n<div>one</div>\n<div>two</div>\n<div>three</div>");

// checking :
$("<textarea/>").val($("body").html()).appendTo("body");

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Pedro ChagasWebmasterAuthor Commented:
Hello again,
I don't understand your solution.
First, I generate dynamic code, because that I don't known where I have to make the line break, so the script have to detect "</div>", and do automatically the line break in every </div>.
Did I express myself wrong?

~JC
leakim971PluritechnicianCommented:
Try this :

codeToBeautify = codeToBeautify.split("</div>").join("</div>\n");

Open in new window


testing

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pedro ChagasWebmasterAuthor Commented:
Good morning.
I will try your solution this night.
Please wait.

~ JC
Pedro ChagasWebmasterAuthor Commented:
Thank you @leakim!
Regards.
leakim971PluritechnicianCommented:
you welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.