Skip to content

Instantly share code, notes, and snippets.

@svondervoort
Last active March 2, 2021 10:24
Show Gist options
  • Save svondervoort/583f4e1592bf079c43a8ea471eaaec00 to your computer and use it in GitHub Desktop.
Save svondervoort/583f4e1592bf079c43a8ea471eaaec00 to your computer and use it in GitHub Desktop.
// Call this function with a form as parameter everytime an input changes
function checkConditions(form) {
let conditions = [];
form.find('div[data-conditional] :input').each(function(){
let value = $(this).val();
conditions.push(value);
});
$.each(conditions, function( index, value ) {
form.find('div[data-condition]').hide();
form.find('div[data-condition="' + value + '"]').each(function(){
$(this).show();
});
});
}
// Example:
const form = $('form[name="my-form"]');
form.find(':input').change(function(){
checkConditions(form);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment