Skip to content

Instantly share code, notes, and snippets.

@jonwaldstein
Created July 5, 2018 21:59
Show Gist options
  • Save jonwaldstein/5feb44d1f982adca2240305eaa894bb5 to your computer and use it in GitHub Desktop.
Save jonwaldstein/5feb44d1f982adca2240305eaa894bb5 to your computer and use it in GitHub Desktop.
Gravity Forms add aria-describedby to fields on validation
jQuery(document).bind('gform_post_render', function(){
let InvalidFields = document.querySelectorAll('.gform_wrapper *[aria-invalid="true"]');
let InvalidFieldsArray = Array.from(InvalidFields, (field) => {
let validationMessages = field.parentElement.parentElement.querySelectorAll('.validation_message');
let fieldID = field.id;
let fieldIDArray = [];
var count = 0;
let ValidationMessagesArray = Array.from(validationMessages, (message) => {
if (validationMessages.length > 1){
fieldID = fieldID+count++;
fieldIDArray.push('validation_message-'+fieldID);
}
message.setAttribute('id', 'validation_message-'+fieldID);
});
if (validationMessages.length > 1){
field.setAttribute('aria-describedby', fieldIDArray.join(" "));
} else {
field.setAttribute('aria-describedby', 'validation_message-'+fieldID);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment