We use this JavaScript method to create a new HTML element:
document.createElement('tagName')
'tagName' with the HTML tag you want to create, like 'div', 'p', 'button', etc.// Step 1: Create the paragraph element
const newParagraph = document.createElement('p');
// Step 2: Add text inside the paragraph
newParagraph.textContent = 'This is a new paragraph added by JavaScript.';
// Step 3: Add the paragraph to the page inside the body
document.body.appendChild(newParagraph);
| Step | What to do |
|---|---|
| 1 | Use document.createElement('tagName') to make a new element. |
| 2 | Add content or attributes (text, id, class). |
| 3 | Attach the element to the page with .appendChild() or .insertBefore(). |
Task -
<button> element with text "Click Me" and add it below the existing content.Final Hard Task
<ul> (unordered list) element.<li> elements with names of your favorite fruits.innerText