JS DOM: insert Adjacent Element
node.insertAdjacentElement(position, newNode)-
Move a element and insert it adjacent to node's begin/end tags.
Return the element inserted, or
nullif failed.position is a string, and must be one of:
"beforebegin"β Before the beginning tag. (as previous sibling.)"afterbegin"β After the beginning tag. (as first child.)"beforeend"β Before end tag . (as last child)."afterend"β After the tag. (as next sibling.)
π’ TIP: use this to attach element, more flexible than
appendChildorinsertBefore// create new node p containing hi const y = document.createElement("p"); y.textContent = "hi"; // a existing node const x = document.getElementsByTagName ("p")[0] ; // insert after x. insertAdjacentElement("afterend", y);
Basic DOM Element Methods
- JS DOM: Node, Node Type, Element
- JS DOM: Get Element by ID, Name, Class etc
- JS DOM: Get Current Script Element
- JS DOM: Get Element Parent, Child, Sibling
- JS DOM: NodeList vs HTMLCollection
- JS DOM: Iterate HTMLCollection
- JS DOM: Element Attribute Methods
- JS DOM: Class Attribute (get add remove toggle)
- JS DOM: Create Element, Clone
- JS DOM: Insert, Remove, Replace Element
- JS DOM: insert Adjacent Element
- JS DOM: appendChild
- JS DOM: textContent, innerHTML, innerText, nodeValue
- JS DOM: Remove Element
- JS DOM: Remove All Children
- JS DOM: Change Element Content
- JS DOM: Add, Remove Event Handler