Skip to content

Instantly share code, notes, and snippets.

@OndrejSlamecka
Created August 16, 2012 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OndrejSlamecka/3371245 to your computer and use it in GitHub Desktop.
Save OndrejSlamecka/3371245 to your computer and use it in GitHub Desktop.
Manipulate DOM class with CoffeeScript
# DOM Class - based on http://www.openjs.com/scripts/dom/class_manipulation.php
class @DomClass
this.has = (el, className) ->
return el.className.indexOf(className) != -1
this.add = (el, className) ->
if !this.has(el, className)
el.className += ' ' + className
this.remove = (el, className) ->
if this.has(el, className)
regex = new RegExp('(\\s|^)' + className + '(\\s|$)')
el.className = el.className.replace(regex, ' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment