Creating elements
DOM interfaces
Every kind of DOM node is represented by an interface based on Node interface.
Element, Document and DocumentFragment interfaces inherit from Node. More about DOM interfaces here
Finding elements
1. querySelector()
document.querySelector(selector) : Element | null
This method returns the first element that matches the CSS selector. If no match is found, it returns null.
More
Also available on Element and fragment
If you want to look within a specific element, just use
ele.querySelector() on the parent element.If you want to look within a specific fragment, just use
df.querySelector() on the parent fragment.2. querySelectorAll()
document.querySelectorAll(selector) : NodeList
This method returns all the elements that match the specified CSS selector.
It returns a static NodeList, which contains the matching elements. If no match is found, it returns empty NodeList.
How to consume NodeList?
NodeList is a iterable, so it can be iterated using for..of loop, or converted to a array using spread syntax like [...nodelist].
Here, the NodeList is static - meaning any changes in the DOM later does not affect the content of the existing collection. More about NodeList here.
querySelectorAll is also available on Element and DocumentFragment, in the same way as querySelector.
Create / insert element
3. createElement()
document.createElement(tagName) : Element
This method creates a new HTML element, taking the specified HTML tag name as argument.
Related
Creating text node
4. setAttribute()
Element.setAttribute(name, value)
This method sets an attribute on the current element.
name - name of the attribute to add. Ex- "type".
value - value of the attribute. Ex- "text".
5. appendChild()
Node.appendChild(newNode)
This method inserts a new node as the last child of the parent node.
Edge cases and alternative
If it already has a parent
If it is DocumentFragment
.append()
⭐️ .append() allows you to insert multiple child elements in a single call. It also allows string as input, which gets converted to Text node.
💔 .appendChild() only allows inserting a single element at a time.
6. removeChild()
Node.removeChild(childNode)
This method tries to remove a child node from the parent and returns the removed node.
If the node to be removed is not a child of the parent node, it throws a NotFoundError DOMException.
7. insertBefore()
Node.insertBefore(newEle, refEle)
This method inserts a new child node before another node within a parent.
It also works with DocumentFragment, by inserting all of its content in that position.
newEle - The node to be inserted.
refEle - The reference node before which newEle should be inserted. If refEle is null, then newEle is added as the last child.
insertAfter()
There is no insertAfter() method. It can be emulated by combining insertBefore method with ele.nextSibling.
8. Document Fragment
document.createDocumentFragment() : DocumentFragment
This method is used to create a new empty document fragment.
DocumentFragment can be considered to be a off-screen lightweight document object.
It is generally used for batching multiple operations - by doing them in the fragment over time and then rendering the whole content to the visible DOM in one shot .
Because fragment is not rendered, making changes to it doesn't cause any performance impact (no reflow). To actually "render" the fragment in the visible document, use appendChild or insertBefore.
In the next section, we'll see how to read information from a DOM element.