Created
November 5, 2014 13:09
-
-
Save azeemhassni/64a0d46e8c4582a1d3a5 to your computer and use it in GitHub Desktop.
Conditional Form Fields
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
jQuery(function($){ | |
// Using : data-conditional='.currently-working;No;hide' data-default-display='none' | |
// Change Default status | |
$('[data-default-display]').each(function(){ | |
if($(this).attr('data-default-display') == 'none') { | |
$(this).css("display","none"); | |
} else { | |
$(this).css("display","inherit"); | |
} | |
}); | |
$("[data-conditional]").each(function(){ | |
var theElement = $(this); | |
var value = $(this).attr('data-conditional'); | |
var values = value.split(';'); | |
var selector = values[0]; | |
var targetValue = values[1]; | |
var action = values[2]; | |
console.log(values); | |
$('body').on('change',selector, function(){ | |
console.log("Current Value : "+ $(selector+":checked").attr('value')); | |
if($(selector+":checked").attr('value') == targetValue) { | |
console.log(action); | |
theElement[action](); | |
} else { | |
var secondAction = (action == 'hide') ? 'show' : 'hide'; | |
console.log(secondAction); | |
theElement[secondAction](); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment