/ Published in: JavaScript

These are handy when not using a library
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Element.prototype.hasClassName = function(name) { return new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)").test(this.className); }; Element.prototype.addClassName = function(name) { if (!this.hasClassName(name)) { this.className = this.className ? [this.className, name].join(' ') : name; } }; Element.prototype.removeClassName = function(name) { if (this.hasClassName(name)) { var c = this.className; this.className = c.replace(new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)", "g"), ""); } };
URL: http://www.html5rocks.com/tutorials/dnd/basics/
Comments
