Skip to content

Instantly share code, notes, and snippets.

@frankstallone
Last active August 29, 2015 14:07
Show Gist options
  • Save frankstallone/b77afd389f56afad4731 to your computer and use it in GitHub Desktop.
Save frankstallone/b77afd389f56afad4731 to your computer and use it in GitHub Desktop.
Toggles hidden classes and disables/enables input fields based on user select choice
function toggleIdent(v) {
'use strict';
// Defining all varables at the top
var value, ssnField, ssnFieldConfirm, taxField, taxFieldConfirm, socialParent, taxParent;
// Value of selector selection
value = $(v).val();
// Finding all the form fields (Used to enable/disable them)
ssnField = $('#alinecard-oc-social');
ssnFieldConfirm = $('#alinecard-oc-social-confirm');
taxField = $('#alinecard-oc-tax-id');
taxFieldConfirm = $('#alinecard-oc-tax-confirm');
// Finding the parent container for X fields (Used to show/hide them)
socialParent = ssnField.closest('.clearfix');
taxParent = taxField.closest('.clearfix');
// If SSN/Tax ID was selected, and it's hidden, set fields to disabled false, toggle hidden classes, set SSN/Tax ID fields to disable true
if (value === "Social Security Number") {
$([taxField, taxFieldConfirm]).each(function(){
$(this).attr('disabled', true);
});
if ($(socialParent).hasClass('hidden') === true) {
$([ssnField, ssnFieldConfirm]).each(function(){
$(this).attr('disabled', false);
});
$([socialParent]).each(function(){
$(this).toggleClass('hidden');
});
}
// Check to see if Tax ID is hidden, if not hide!
if (taxParent.hasClass('hidden') === false) {
$([taxParent]).each(function(){
$(this).toggleClass('hidden');
});
}
} else if (value === "Tax Identification Number") {
$([ssnField, ssnFieldConfirm]).each(function(){
$(this).attr('disabled', true);
});
if ($(taxParent).hasClass('hidden') === true) {
$([taxField, taxFieldConfirm]).each(function(){
$(this).attr('disabled', false);
});
$([taxParent]).each(function(){
$(this).toggleClass('hidden');
});
}
if (socialParent.hasClass('hidden') === false) {
$([socialParent]).each(function(){
$(this).toggleClass('hidden');
});
}
} else {
alert('Please select a valid option. Either Social Security Number or Tax Identification Number.');
};
}
// Run toggleIdent() on select change
$('#alinecard-order-id').on('change', function(){
'use strict';
toggleIdent(this);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment