document.createElement() is a JavaScript method that creates a new HTML element node with the
specified tag name. The created element exists in memory and must be appended to the DOM to be visible.
div, img, p) dynamically.setAttribute(), classList.add(), or
innerText.append() or
appendChild().const element = document.createElement(tagName);
tagName: A string specifying the element type (e.g., 'div', 'img',
'p').
Returns: A new HTML element node.
Create a div, add a class, set text content, and append it to the body:
const div = document.createElement('div');
div.classList.add('my-class');
div.innerText = 'Hello, World!';
document.body.append(div);
This example generates Pokémon images with their index numbers based on user input.
Element.remove() is a JavaScript method that removes an element (and its children) from the DOM.
It is called directly on the element, requiring no reference to its parent.
parentNode.removeChild() for older browsers.
element.remove();
element: The DOM element to remove.
No parameters or return value.
Remove a specific div element:
const div = document.querySelector('.my-div');
div.remove();