Last active
August 29, 2015 14:06
-
-
Save TechFounder/56cf130a47273bb5b304 to your computer and use it in GitHub Desktop.
CoffeeScript to use with checkbox to hide / unhide form fields or div.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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