DEV Community

artydev
artydev

Posted on

HTML string to DOM

Awesome little snippet toDom from William Malo

String.prototype.toDOM = function () {
    var d = document,
        i, a = d.createElement("div"),
        b = d.createDocumentFragment()
    a.innerHTML = this
    while (i = a.firstChild) b.appendChild(i)
    return b
}

const mail = (name) => `Hello ${name}`

const mailToHal = mail("Hal").toDOM()

document.body.append(mailToHal)

You can test it here : toDom

Top comments (0)