Possible Duplicate:
In Javascript, can you extend the DOM?
I'm trying to add methods and properties to the Element. It is not listed as a global class. I can extend String like this:
String.prototype.dosomething = function { ... };
I try like this (it's bigger, this is the basic):
function $id(str){
this.element = document.getElementById(str);
return element;
}
var myElem = $id('myId'); // WORKS !!!
$id.prototype.dosomethig = function ( ... };
var myValue = $id('myId').dosomething(); // DOESN'T WORK !!!
I try in other ways but the point it's always the same, when I try to extend the class, it doesn't work. Is there a way to avoid this? I know jQuery do this, but I want to do my onwn - if they can, I can... right?
Edit: Below a code already working in Safari and Firefox...
Element.prototype.pos = function(){
var curleft = this.offsetLeft;
var curtop = this.offsetTop;
var scrleft = this.scrollLeft;
var scrtop = this.scrollTop;
var pElement = pElementScr = this.offsetParent
while(pElement){
curleft += pElement.offsetLeft;
curtop += pElement.offsetTop;
pElement = pElement.offsetParent;
}
while(pElementScr){
scrleft += pElementScr.scrollLeft;
scrtop += pElementScr.scrollTop;
pElementScr = pElementScr.offsetParent;
}
return {x:this.offsetLeft, y:this.offsetTop};
}
It gives the position of a div even inner of a lot of other divs. I'll try to test in IE8, after solve a lot of other issues of my library.