+ 1
How Do I Write Extension Method in Javascript for this Case?
So... I want to write my own "jQuery" to simplify DOM for my purpose. So, I wrote this code https://code.sololearn.com/W34vh67EoI0A/?ref=app I'm expecting this code to change the text in paragraph with id "world" from "Hello" to "You are awesome" using command Dom.Id("id").text("text"); But somehow it gave me an error. Please help me to fix this.
5 Respuestas
+ 1
class Dom
{
static Id(id)
{
return document.getElementById(id);
}
}
Object.defineProperty(HTMLElement.prototype, "text",
{
value: function text(x)
{
this.innerHTML = x;
}
});
Object.defineProperty(HTMLElement.prototype, "html",
{
value: function html(x)
{
this.outerHTML = x;
}
});
window.onload = function()
{
Dom.Id("world").text("You are awesome!");
}
+ 1
Mohamed ELomari Wow, thanks! It worked😀
+ 1
Oh yeah, Mohamed ELomari, one more thing, so this mean I should use String.prototype if I want to return string and HTMLElement.prototype if I want to modify HTML element
Is there any SomethingElse.prototype that I can use?
+ 1
depending on the the type return from Dom::Id method
+ 1
Mohamed ELomari Is there any list of that?