Added HTML Element Classes

This commit is contained in:
2018-03-13 05:15:42 +11:00
parent 430f9ca987
commit 117cf3784a

32
common/HTMLElement.js Normal file
View File

@ -0,0 +1,32 @@
HTMLElement.prototype.removeClass || (HTMLElement.prototype.removeClass = function(remove) {
var newClassName = "";
var i;
var classes = this.className.split(" ");
for(i = 0; i < classes.length; i++) {
if(classes[i] !== remove) {
newClassName += classes[i] + " ";
}
}
this.className = newClassName;
});
HTMLElement.prototype.hasClass || (HTMLElement.prototype.hasClass = function(cls) {
return (' ' + this.className + ' ').indexOf(' ' + cls + ' ') > -1;
});
HTMLElement.prototype.addClass || (HTMLElement.prototype.addClass = function(add) {
if(this.hasClass(add)) return;
this.className += ' ' + add;
});
HTMLElement.prototype.toggleClass || (HTMLElement.prototype.toggleClass = function(clazz) {
if(this.hasClass(clazz)) {
this.removeClass(clazz);
} else {
this.addClass(clazz);
}
});
HTMLElement.prototype.remove || (HTMLElement.prototype.remove = function() {
this.parentElement.removeChild(this);
});