I'm trying to append a line of HTML before all the children of the body
.
Right now I have this:
// Prepend vsr-toggle
var vsrToggle = document.createElement("div");
vsrToggle.innerHTML = "<input type='checkbox' name='sr-toggle' id='srToggle'><label for='srToggle' role='switch'>Screen reader</label>"
document.body.insertBefore(vsrToggle, pageContent);
It's working fine because the HTML is being added to the created div
. However, I need to prepend this element without wrapping it in a div.
Is there a way to prepend the HTML without first creating an element? If not, can I create the input
as a self-closing element and append the label
to it?
Is there a better way to achieve this?
Cheers!
insertBefore()
to insert the<input>
, then call it again to insert the<label>
. Self-closing has nothing to do with this. – Awfully