Example 1: Creating a div element
Say you just want to create a div element, then for that below is the code snippet:
let div_element = document.createElement("div")
Example 2: Adding a child node to an HTML element
Say there’s a div with ID “divele”
And you want to add a paragraph element inside div. Below is the code snippet
This code snippet is adding a paragraph element inside div with the help of appendChild() method.
Example 3: Removing a child node of an HTML element
Consider an unordered list having 3 list items in it. And you want to remove the first child <li> element in it.
Using the removeChild() method, we can remove the first <li> child from the unordered list:
Example 4: To access all the child elements of a parent HTML element
Now, to access all the child elements of the div having id “divele”, we will use the childNodes() method which will return the required child nodes.
Please find below the snapshot of the simple webpage that we are going to develop.