2

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.

isherwood
  • 58,414
  • 16
  • 114
  • 157
NickOlder
  • 37
  • 1
  • 3
  • 1
    What does this have to do with ajax? Is the HTML _returned_ from an ajax call? – isherwood Apr 29 '22 at 13:59
  • To do what you ask (*"keep the text uncondensed"*), see [this answer](https://stackoverflow.com/a/805113/2181514) - however it (and the *numerous* other duplicates) are a little out of date and you've also asked *is there another [way to do this]* which is succinctly answered below: use ` – freedomn-m Apr 29 '22 at 15:50

4 Answers4

3

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.

let $template = $($.find("#template1")[0].innerHTML)

$(".parent").append($template);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="parent">

</div>

<template id="template1">
    <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>
</template>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
1

You can use a template element to hold the html.

const template1 = document.querySelector("#myTemplate");
document.querySelector(".add").addEventListener("click", function () {
  const elements = template1.content.cloneNode(true);
  document.querySelector(".out").appendChild(elements);
});
.out > div {
  border: 1px solid black;
}
<template id="myTemplate">
<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>
</template>

<button class="add">Add</button>
<div class="out"></div>

Other option is to use a string template literal

var myTemplate = `
<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>
`;

document.querySelector(".add").addEventListener("click", function() {
  document.querySelector(".out").insertAdjacentHTML('beforeend', myTemplate);
});
.out>div {
  border: 1px solid black;
}
<button class="add">Add</button>
<div class="out"></div>
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

Whenever I use jQuery in a project I always create a couple of extension functions for this express purpose.

function createElement(tag) {
    return $('<' + tag + '>');
}

function createElement(tag, className) {
    return $('<' + tag + '>').addClass(className);
}

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:

$(".parent").append("<div class="child">Text</div>");

Into this:

$('.parent').append(createElement('div', 'child').text('Text'));

To append something more like this:

<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>

You might do:

$('.appendTarget')
    .append( 
        createElement('div', 'mt-5 mb-4 d-flex justify-content-center align-items-center')
            .append(
                      createElement('div', 'col-md-5')
                           .append(
                                createElement('p').text('My Silly Text'),
                                createElement('i', 'some-icon')
                           ),
                       createElement('div','col-md-5')
                           .append(
                                    createElement('p').text('more text'),
                                    createElement('div').text('Description')
                                  )
                   )
     );
Andrew Corrigan
  • 1,017
  • 6
  • 23
-2

Please, do not use jquery if you can.

Show this simple example:

const ul = document.getElementById('list');
const child = document.createElement('li');

// if you need to add css class:
// child.classList.add('item');

child.textContent = 'my first li!';

ul.appendChild(child);

✌️