+ 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.

21st May 2018, 11:03 PM
Keanu Taufan
Keanu Taufan - avatar
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!"); }
21st May 2018, 11:25 PM
MO ELomari
MO ELomari - avatar
+ 1
Mohamed ELomari Wow, thanks! It worked😀
22nd May 2018, 4:11 AM
Keanu Taufan
Keanu Taufan - avatar
+ 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?
22nd May 2018, 4:57 AM
Keanu Taufan
Keanu Taufan - avatar
+ 1
depending on the the type return from Dom::Id method
23rd May 2018, 1:34 AM
MO ELomari
MO ELomari - avatar
+ 1
Mohamed ELomari Is there any list of that?
23rd May 2018, 4:19 AM
Keanu Taufan
Keanu Taufan - avatar