This is a small library for extending HTMLElement
methods.
Returns the first descendant of the element which matches the given selector. This is identical to Element.querySelector()
.
<HTMLElement> HTMLElement.descendant(<String> selector)
Returns the first ancestor of the element which matches the given selector. Opposite of Element.descendant()
.
<HTMLElement> HTMLElement.ancestor(<String> selector)
Returns true
if the element this is invoked on is a descendant of the given element.
<Boolean> HTMLElement.descendantOf(<HTMLElement> ancestorNode)
Returns true
if the element this is invoked on is an ancestor of the given element. Opposite of Element.descendantOf()
.
<Boolean> HTMLElement.ancestorOf(<HTMLElement> descendantNode)
Can also be called on document
.
Deletes the given element from the DOM
. Identical to HTMLElement.parentElement.removeChild(HTMLElement)
.
<void> HTMLElement.delete()
Can also be called on document
.
Inserts the given element to the top/start of the desired parent element, before its first child. Identical to parentElement.insertAdjacentElement('AfterBegin', newChild)
.
<HTMLElement> parentNode.prepend(<HTMLElement> newChild)
Can also be called on document
.
Inserts the given element to the bottom/end of the desired parent element, after its last child. Identical to parentElement.insertAdjacentElement('BeforeEnd', newChild)
and parentElement.appendChild(newChild)
.
<HTMLElement> parentNode.append(<HTMLElement> newChild)
Can also be called on document
.
Inserts the given element before the element this is invoked on, and placed within its parent. Identical to element.insertAdjacentElement('BeforeBegin', newSibling)
.
<HTMLElement> element.before(<HTMLElement> newSibling)
Can also be called on document
.
Inserts the given element after the element this is invoked on, and placed within its parent. Identical to element.insertAdjacentElement('AfterEnd', newSibling)
.
<HTMLElement> element.after(<HTMLElement> newSibling)
Can also be called on document
.
Waits for the first element matching the given selector to be available in the DOM
.
<Promise<HTMLElement>> document.waitForChild(<string> selector[, <number> timeout])
document.waitForChild('div', 3000).then(div => {
div.innerHTML = 'Hello world!';
});
The timeout defaults to 60 seconds (60000 ms).
Creates a new element with the given properties.
<HTMLElement> document.create(<String> HTMLTag)(<Object> properties[, <Object> options)
let button = document.create('button')({
class: 'btn btn-md btn-primary',
textContent: 'Yoo-hoo!',
onClick: function() {
this.textContent = 'Big summer blowout!';
}
});
document.body.append(button);
This method extends document.createElement()
; it accepts both parameters. The options
parameter is an optional second parameter of the second call ({properties[, options]})
.