DEV Community

Discussion on: Re-implementing jQuery methods in the HTMLElement prototype

 
jochemstoel profile image
Jochem Stoel

Well whatta ya know. You are right.
Change the line

if (!string)

into this

if (typeof string == 'undefined')

so that you get

window.$ = (query, ctx = document) => ctx.querySelector(query)
window.$$ = (query, ctx = document) => ctx.querySelectorAll(query)

HTMLElement.prototype.html = function (string) {
    if (typeof string == 'undefined')
        return this.innerHTML
    this.innerHTML = string
    return this
}

$('#test').html('');

codepen.io/jochemstoel/pen/OrJLbR

You can also replace it with

if(typeof string != 'string')

or even

if(string == undefined)