.prepend()
void ParentNode.prepend((Node or String)... nodes)
Warning! Current browsers do not yet support
.prepend()
, please
use a polyfill
until they do.
Prepend one or more nodes to the end of a node’s child list.
node.prepend(nodes);
Inserts nodes before the first child of node, while replacing strings in nodes with equivalent text nodes.
See also: .append()
Example
HTML:
<p>
All
<strong>large</strong>
goats
<em>are</em>
delicious!
</p>
JavaScript:
let el = document.createElement("strong");
el.textContent = "I";
document.query("p").prepend(el, " think:");
Resulting HTML:
<p>
<strong>I</strong>
think:
All
<strong>large</strong>
goats
<em>are</em>
delicious!
</p>