In Javascript, you can extend existing classes by using its prototype object:
String.prototype.getFirstLetter = function() { return this[0]; };
Is it possible to use this method to extend DOM elements?
I found the answer just as I was writing the question, but thought I'd post anyway to share the info.
The object you need to extend is Element.prototype
.
Element.prototype.getMyId = function() { return this.id; };
you can extend the DOM by using the Element's prototype. However, this does not work in IE7 and earlier. You will need to extend the specific element, one at a time. The Prototype library does this. I recommend looking through the source to see exactly how it's done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With