I need to dynamically create element using DOM. I know how to do it using document.createElement, appendChild methods, but this is often a long process.
Suppose that I want to add this in my HTML page :
<div id="my_account" style="visibility:hidden">
<input id="my_account_close" type="submit" value="Close" onclick="closeMyAccount();">
<span id="my_account_username">Blabla</span>
<input id="my_account_username_modify" type="submit" value="Change Name" onclick="modifyUserName();">
<span id="my_account_email">a@asqs.com</span>
</div>
Do I have to create each element one by one and set all attributes, then use the appendChild methods ?
Is there a “magic” function that can take the html code which returns the parent element, so that I only have to add it at the end?
no, you can just take that entire string and add it as innerHTML to a parent div.
It will be part of the dom on the very next line of code…