Skip to content

Instantly share code, notes, and snippets.

@zdavis
Last active August 29, 2015 14:05
Show Gist options
  • Save zdavis/b713e3eb96e94e90834b to your computer and use it in GitHub Desktop.
Save zdavis/b713e3eb96e94e90834b to your computer and use it in GitHub Desktop.
A simple CCS-based switcher in JS
<input type="checkbox" value="1" />
class Switcher
construct: ($el, options) ->
@$el = $el
@$switcherEl = 'some template containin markup for the switcher'
@$el.append(@$switcherEl)
@state = @$el.prop('checked')
@initEvents()
initEvents: () ->
@$el.click(() => @toggle)
changeState: (to) ->
if to == true
@$switcherEl.addClass('checked')
@$el.attr('checked', true)
else
@$switcherEl.removeClass('checked')
@$el.attr('checked', false)
toggle: () ->
if @state == true
@changeState(false)
else
@changeState(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment