loading...

HTML string to DOM

artydev profile image artydev ・1 min read

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

Posted on by:

Discussion

pic
Editor guide