Skip to content

Instantly share code, notes, and snippets.

@jlittlejohn
Created March 25, 2014 20:21
Show Gist options
  • Save jlittlejohn/9770529 to your computer and use it in GitHub Desktop.
Save jlittlejohn/9770529 to your computer and use it in GitHub Desktop.
JS: Toggle Required Attribute based on Checkbox
// 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