Skip to content

Instantly share code, notes, and snippets.

@TechFounder
Last active August 29, 2015 14:06
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 TechFounder/56cf130a47273bb5b304 to your computer and use it in GitHub Desktop.
Save TechFounder/56cf130a47273bb5b304 to your computer and use it in GitHub Desktop.
CoffeeScript to use with checkbox to hide / unhide form fields or div.
# Use this if you only want to use the checkbox as a clicker
$('#checkbox-id').click ->
$('.what-you-want-to-hide').slideToggle('slow')
# Use this if you actually care about checking to see if the checkbox has been checked
$('#checkbox-id').click ->
if $(@).prop('checked')
$('.what-you-want-to-hide, .what-you-want-to-hide-also').slideDown 'slow'
else
$('.what-you-want-to-hide, .what-you-want-to-hide-also').slideUp 'slow'
# This version creates a variable to use so that it's DRY
# If you wish for this to persist in your Rails app,
# you also have to add an attr to the model.
attr_accessor :checkbox-id
# and, you have to have this line for simple form
= f.input :checkbox-id, as: :boolean
fieldVisibility = ->
if $('#checkbox-id').prop('checked')
$('.what-you-want-to-hide').slideDown 'slow'
else
$('.what-you-want-to-hide').slideUp 'slow'
$('#checkbox-id').click ->
fieldVisibility()
fieldVisibility()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment