/ Published in: JavaScript
URL: http://www.html5rocks.com/tutorials/dnd/basics/
These are handy when not using a library
Expand |
Embed | Plain Text
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"), ""); } };
Comments
Subscribe to comments
You need to login to post a comment.

Hi Axolx,
I noticed that if for some crazy reason you had the same classname in triplicate or more your 'replace' regex would fail, i.e. class="nick nick nick nick nick", only the 1st, 3rd and 5th elements would be selected so I though you could possible use look-behinds/aheads below rather than non-capturing groups
RegExp("(?
RegExp('(?
I would post the code but it doesn't want to let me:(