daGrevis
daGrevis

Reputation: 21333

How To Append (Or Other Method) a Lot of HTML Code?

I need to append a lot of HTML code. To improve readability, I don't want to write that all in one line, but split them as regular HTML. That would be like 15 new-lines or something. The problem is that JavaScript doesn't allow me to do that.

var target = $('.post .comment_this[rel="' + comment_id + '"]').parent().parent();

target.append('
    <div class="replies">
        <p>...</p>
        <img src="" alt="" />
    </div>
');

Basically, I need to add HTML in that place.

I need to add some variables too.

Upvotes: 9

Views: 5053

Answers (6)

Jonathan
Jonathan

Reputation: 7098

As of 2015, ECMA 6, you can do this:

target.append(`
    <div class="replies">
        <p>...</p>
        <img src="" alt="" />
    </div>
`);

Upvotes: 0

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17576

    target.append(' <div class="replies">'+
            '<p>...</p>'+
            '<img src="" alt="" />'+
        '</div>'
    );

or

    target.append(' <div class="replies">\
            <p>...</p>\
            <img src="" alt="" />\
        </div>'
    );

Upvotes: 11

user555600
user555600

Reputation: 164

use html() instead of append

target.html('<div class="replies"><p>...</p><img src="" alt="" />,</div>');

Upvotes: -1

MarrLiss
MarrLiss

Reputation: 808

If you want to insert variables to html, you can use some templating library like jQuery.template

Upvotes: 0

Patrick R
Patrick R

Reputation: 1979

target.append(' ?>
    <div class="replies">
        <p>...</p>
        <img src="" alt="" />
    </div>
<?');

Separate the html and php with the close/open php tags and it should work fine.. when adding var's in the html, just use the tags again, like this: <? $hello ?>

Upvotes: 1

Related Questions