If you do this repeatedly (dynamically creating HTML), you can use a more general approach.
If you want to create three unrelated elements, you can do:
var anchor = elem("a", {"id":"id1"}); var div = elem("div", {"id":"id2"}); var xyz = elem("div", {"id":"id3"});
You now have three elements. If you want to get the HTML code (as a string), simply do:
var html = anchor.outerHTML + div.outerHTML + xyz.outerHTML;
If you want to have these three elements (say a div), do:
var div = elem("div", null, [ elem("a", {"id":"id1"}), elem("div", {"id":"id2"}), elem("div", {"id":"id3"}), ]);
You can get HTML using div.outerHTML or just add it anywhere.
To learn more about elem() , visit element.js (GitHub) .
I am adding this answer not for an 8-year-old question, but for future visitors. Hope it helps.
source share