I want to convert jquery to lightweight js. But I get error.
Like I want to use jquery's $('#app').html('test');
var $ = function (method_name) {
return document.querySelector(method_name);
}
$.prototype.html = function (value) {
this.innerHTML = value;
};
$('#app').html('test'); // <--- .html() is not function
How to add .html() in $()?
only $('#app').innerHTML = 'test' is working
$returns an element and not a$object.$function is not even working properly, it would fail for non ID selectors..prototypeonly makes sense if you used$withnew.$("#app")is anHTMLElement; these don’t have anhtmlmethod. You could create an instance withnewinside the$function (e.g. by checkingnew.target), but this can get complex very quickly. Why not simply use the DOM API directly?