To create a document in JavaScript, first, we need to create a new HTML document using the createElement()
method, then we can add elements to it using the appendChild()
method. Finally, we can add text to the elements using the innerHTML
property.
Here is an example code snippet to create a new document with a h1
and a p
element:
index.tsx722 chars24 lines
In this example, we first create a new HTML document using createElement('html')
. Then we create new head
and body
elements using createElement()
, and we add them to the html
element using appendChild()
.
Next, we create a new h1
element and set its innerHTML to 'Hello World'. We add this element to the body
element using appendChild()
. Similarly, we create a new p
element and add it to the body
.
Finally, we add the head
and body
elements to the html
element using appendChild()
, and output the new document to the console using console.log()
.
gistlibby LogSnag