Inserting New HTML Elements via JavaScript
How to add a new element to HTML DOM in JavaScript?
To add a new element to the HTML DOM, we have to create it first and then we need to append it to the existing element.
Steps to follow
1) First, create a div section and add some text to it using <p> tags.
2) Create an element <p> using document.createElement("p").
3) Create a text, using document.createTextNode(), so as to insert it in the above-created element("p").
4) Using appendChild() try to append the created element, along with text, to the existing div tag.
Thus a new element is created(<p>) and appended to the existing element(<div>).
Comments
Post a Comment