0

I am struggling to append huge amounts of html code. I found this answer How To Append (Or Other Method) a Lot of HTML Code? which I followed but I am getting an illegal argument error. Can anyone suggest a better way to manage this?

CODEPEN DEMO

JS

$(function() {
  $("a").click(function() {
    $("body").append(codeBlock);
  });
});

var codeBlock = $("<div class='code-block'>\
    <div class='select-event'>\
        <label>Start</label>\
        <select>\
            <option>when the Run button is pressed</option>\
            <option>when the object is pressed</option>\
            <option data-option='keyoption'>when a keyboard key is pressed</option>\
        </select>\
        <span class='key-press-wrapper'>\
            <label>Key</label>\
            <input id='keyPress' placeholder='Press Key'>\
        </span>\
    </div>\
    <div class='insert-block'>\
        <label>Insert code blocks here</label>\
        <ul id='sortable' class='connectedSortable'></ul>\
    </div>\
</div>\");
Community
  • 1
  • 1
2ne
  • 5,766
  • 9
  • 30
  • 51

3 Answers3

3

I am getting an illegal argument error.

</div>\");

That's an odd error, probably it should be "not a valid string literal" or some kind of that. You're escaping the ending string delimiter, which you shouldn't. Remove the backslash in the last line.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

Remove last slash at </div>\")

Chris
  • 3,405
  • 4
  • 29
  • 34
2

Using a template engine is definitely the best solution.

e.g. https://github.com/janl/mustache.js

charlee
  • 1,309
  • 1
  • 11
  • 22