HTML code for this is:
First, we see the div ‘presentdiv’. It has two nodes, first is the text node and another is input ‘button’. New div is created on clicking button. How?
6: A new div is created using createElement(). It takes the HTML tagname of element to be created as argument.
7: setAttribtue() sets the attribute of the element using it. Like we have set ‘id’ as i, which is incrementing on every click.
8: innerHTML is used if you want to add text to the element. Mostly used with div.
9: We saved parentelement i.e., ‘presentdiv’ for further use instead of calling again and again.
10: appendChild() is called to append the element we just created, div(i). It appends at the end of the ‘parentelement’.
11: This line is used if we want to add new element at the beginning of parent element. ‘firstChild’ returns the first child of the ‘parentelement’.
12: insertBefore() is used if new element is to be inserted before the some element we know. Here we insert ‘newelement’ before ‘firstchild’.
Also see lastChild and returning values of firstChild and lastChild.