Skip to content

Instantly share code, notes, and snippets.

@mmenavas
Created July 16, 2020 19:46
Show Gist options
  • Save mmenavas/8583291c6afed7545a93eb15b41031ed to your computer and use it in GitHub Desktop.
Save mmenavas/8583291c6afed7545a93eb15b41031ed to your computer and use it in GitHub Desktop.
This snippet expands the details tags whenever a child form field fails browser validation
(function ($, Drupal) {
Drupal.behaviors.openRequiredWrappers = {
attach: function (context, settings) {
// Prevent unnecessary multiple reloads.
if (context instanceof HTMLDocument) {
console.log("Test WDDSE-1913");
var required_items = $('input,textarea,select').filter('[required]');
required_items.each(function (index, el) {
// Add event listener that reacts when a form field fails validation.
$(el).on('invalid', function(e){
// Expand all collapsed wrappers.
$(el).parents("details").each(function(index2, wrapper) {
wrapper.open = true;
});
});
})
}
}
}
})(jQuery,Drupal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment