How to insert multi-line html code into js?

  • 0
    I insert with Jquery my html code
    $('div').append('<div>text<div>text2</div></div>');

    But it is very difficult to read the code when there is a lot of it and it is stuffed into one line. Is it possible to format it somehow conveniently?
    var test = /*
    <div>
        text
        <div>
            text2
        </div>
    </div>
    */;
    $('div').append(test);
    JavaScript Isabelle Hickman, Dec 7, 2020

  • 2 Answers
  • 0
    `<div>
    text
    <div>
    text2
    </div>
    </div>`
    Harper Woods

  • 0
    I usually do this:

    var test = '<div>'+
    'text'+
    '<div>'+
    'text2'+
    '</div>'+
    '</div>';

    $('div').append(test);


    An example with more complex markup:



    P. S. I didn't know that, but it turns out you can use `as quotes, this is probably the best option:

    var test = `
    <div>
    text
    <div>
    text2
    </div>
    </div>
    `;

    $('div').append(test);
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!