Do youself a HUGE favor and DO NOT OVERLOAD THE Object prototype!
You'll only find it a hassle when you go to iterate through the members of an object.
Try the below instead. Just wrap any element ID or element in $(), and it has every method you want on your HTML Elements attached to it. Prototype does something similar here.
var ElementMethods={
hasClass:function(className){ var pattern = new RegExp('(^|\\s)' + className + '(\\s|$)'); return pattern.test(this.className); },
addClass:function(className){ if(!this.hasClass(className)) this.className += (' ' + className); },
removeClass:function(className){ var pattern = new RegExp('(^|\\s)' + className + '(\\s|$)'); this.className = this.className.replace(pattern, ' '); } } var $X(dst,src){for(var i in src) dst[i]=src[i];} var $=function(a){ if(!!a.toLowerCase) a=document.getElementById(a);
$X(a,ElementMethods); return a; }
You'll only find it a hassle when you go to iterate through the members of an object.
Try the below instead. Just wrap any element ID or element in $(), and it has every method you want on your HTML Elements attached to it. Prototype does something similar here.
var ElementMethods={
hasClass:function(className) {
var pattern = new RegExp('(^|\\s)' + className + '(\\s|$)');
return pattern.test(this.className);
},
addClass:function(className) {
if (!this.hasClass(className))
this.className += (' ' + className);
},
removeClass:function(className) {
var pattern = new RegExp('(^|\\s)' + className + '(\\s|$)');
this.className = this.className.replace(pattern, ' ');
}
}
var $X(dst,src) {for (var i in src) dst[i]=src[i];}
var $=function (a){
if (!!a.toLowerCase) a=document.getElementById(a);
$X(a,ElementMethods);
return a;
}