Created
March 25, 2014 20:21
-
-
Save jlittlejohn/9770529 to your computer and use it in GitHub Desktop.
JS: Toggle Required Attribute based on Checkbox
This file contains hidden or 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
// Toggle Required Attributed based on Checkbox | |
(function(){ | |
var checkBox = $('input#acceptTextMessages'); | |
var input = $('input#cellPhoneNumber'); | |
function addRequired () { | |
$(input).attr({required: true}).after('<span class="required">*</span>'); | |
} | |
function removeRequired () { | |
$(input).attr({required: false}); | |
$(input).next().remove(); | |
} | |
function toggleRequired () { | |
if ( $(this).is(':checked') ) { | |
addRequired(); | |
} else { | |
removeRequired(); | |
} | |
} | |
$(checkBox).on( 'click', toggleRequired ); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment