feat: added a prepend function

This commit is contained in:
ThinLiquid 2024-01-18 09:00:48 +00:00
parent 8d3b629185
commit f106776bee
No known key found for this signature in database
GPG key ID: 17538DC3DF6A7387

View file

@ -196,6 +196,24 @@ export default class HTML {
return this
}
/**
* Prepend an element. Typically used as a `.prepend(new HTML(...))` call.
* @param elem The element to prepend.
* @returns HTML
*/
prepend (elem: string | HTMLElement | HTML): HTML {
if (elem instanceof HTMLElement) {
this.elm.prepend(elem)
} else if (elem instanceof HTML) {
this.elm.prepend(elem.elm)
} else if (typeof elem === 'string') {
const newElem = document.createElement(elem)
this.elm.prepend(newElem)
return new HTML(newElem.tagName)
}
return this
}
/**
* Append multiple elements. Typically used as a `.appendMany(new HTML(...), new HTML(...)` call.
* @param elements The elements to append.