After getting success in my AJAX response, I need to append HTML content inside a div
. Usually, I do something like this:
$(".parent").append("<div class='child'>Text</div>");
This is usually fine, except when I have a massive quantity of content that I need to be appended. For example, I need to append this HTML text inside the parent div
:
<div class="mt-5 mb-4 d-flex justify-content-center align-items-center">
<div class="col-md-5">
<p>Text example 1</p>
<i class="some-icon"></i>
</div>
<div class="col-md-5">
<p>Text example 2</p>
<div>Description</div>
</div>
</div>
I can put this text in one line, yes, but it is massively unorganized, confusing and hard to read.
Is there any way to keep the text uncondensed in the append
function? Or is there another jQuery function that allows me not to use this text in one single line?
I would apreciate any help.
4
Answers
Please see the following demo that stores html in a template element. The good thing with a template element is that by design it doesn’t get rendered to the page.
Whenever I use jQuery in a project I always create a couple of extension functions for this express purpose.
They might be included in another library that I’m not aware of by default.
But anyway, with these two functions you can turn this:
Into this:
To append something more like this:
You might do:
You can use a template element to hold the html.
Other option is to use a string template literal
Please, do not use jquery if you can.
Show this simple example:
✌️