How to store a HTML snippet and insert it later in the document?

Just get the HTML code of the div you want by var content = $('#somediv').html(); and then append it to some div later on ! $('#otherdiv').append(content);

$().html(); delivers the HTML Content of that div. documentation: http://api.jquery.com/html/

$().append(<content>); appends the Content to a special div. documentatoin: http://api.jquery.com/append/


You could use javascript templates like (WARNING: Original links broken and/or lead to spam sites: ejs, haml-coffee, ...

Current links (2019-12-14): ejs haml-coffee


You could write:

var mysnippet = "<div class='myclass'>"+
  "<div class='anotherclass'>"+
    "Some dummy text"+
  "</div>"+
"</div>";

and then insert is using the append function (which takes the snippet as argument).