DOM Manipulation
We can split up DOM Manipulation into 3 main categories and 2 supplementary categories:
Selecting Elementsβ
- getElementById() β select an element by id.
- getElementsByName() β select elements by name.
- getElementsByTagName() β select elements by a tag name.
- getElementsByClassName() β select elements by one or more class names.
- querySelector() β select elements by CSS selectors.
Traversing Elementsβ
- Get the parent element β get the parent node of an element.
- Get child elements β get children of an element.
- Get siblings of an element β get siblings of an element.
Manipulating elementsβ
- createElement() β create a new element.
- appendChild() β append a node to a list of child nodes of a specified parent node.
- textContent β get and set the text content of a node.
- innerHTML β get and set the HTML content of an element.
- innerHTML vs. createElement β explain the differences between innerHTML and createElement when it comes to creating new elements.
- DocumentFragment β learn how to compose DOM nodes and insert them into the active DOM tree.
- insertBefore() β insert a new node before an existing node as a child node of a specified parent node.
- insertAfter() helper function β insert a new node after an existing node as a child node of a specified parent node.
- append() β insert a node after the last child node of a parent node.
- prepend() β insert a node before the first child node of a parent node.
- insertAdjacentHTML() β parse a text as HTML and insert the resulting nodes into the document at a specified position.
- replaceChild() β replace a child element by a new element.
- cloneNode() β clone an element and all of its descendants.
- removeChild() β remove child elements of a node.
Working with Attributesβ
- setAttribute() β set the value of a specified attribute on a element.
- getAttribute() β get the value of an attribute on an element.
- removeAttribute() β remove an attribute from a specified element.
- hasAttribute() β check if an element has a specified attribute or not.
Manipulating Elementβs Stylesβ
- style property β get or set inline styles of an element.
- getComputedStyle() β return the computed style of an element.
- className property β return a list of space-separated CSS classes.
- classList property β manipulate CSS classes of an element.
- Elementβs width & height β get the width and height of an element.