Created
July 5, 2018 21:59
-
-
Save jonwaldstein/5feb44d1f982adca2240305eaa894bb5 to your computer and use it in GitHub Desktop.
Gravity Forms add aria-describedby to fields on validation
This file contains hidden or 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(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