Logic to remove stale alerts

This commit is contained in:
zandercymatics 2024-01-18 13:14:13 -07:00
parent 4c5d8b2c55
commit 0a1da49c33
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -236,7 +236,7 @@ function handleValidationClick(e) {
const checkAvailabilityInput = document.getElementById(targetId); const checkAvailabilityInput = document.getElementById(targetId);
checkAvailabilityButton.addEventListener('click', checkAvailabilityButton.addEventListener('click',
function() { function() {
removeFormErrors(checkAvailabilityInput) removeFormErrors(checkAvailabilityInput, true);
} }
); );
} }
@ -248,7 +248,7 @@ function handleValidationClick(e) {
// Only apply this logic to alternate domains input // Only apply this logic to alternate domains input
if (domainInput.classList.contains('alternate-domain-input')){ if (domainInput.classList.contains('alternate-domain-input')){
domainInput.addEventListener('input', function() { domainInput.addEventListener('input', function() {
removeFormErrors(domainInput); removeFormErrors(domainInput, true);
} }
); );
} }
@ -259,7 +259,7 @@ function handleValidationClick(e) {
/** /**
* Removes form errors surrounding a form input * Removes form errors surrounding a form input
*/ */
function removeFormErrors(input){ function removeFormErrors(input, removeStaleAlerts=false){
// Remove error message // Remove error message
let errorMessage = document.getElementById(`${input.id}__error-message`); let errorMessage = document.getElementById(`${input.id}__error-message`);
if (errorMessage) { if (errorMessage) {
@ -284,6 +284,16 @@ function removeFormErrors(input){
parentDiv.classList.remove('usa-form-group--error'); parentDiv.classList.remove('usa-form-group--error');
} }
} }
if (removeStaleAlerts){
let staleAlerts = document.getElementsByClassName("usa-alert--error")
for (let alert of staleAlerts){
// Don't remove the error associated with the input
if (alert.id !== `${input.id}--toast`) {
alert.remove()
}
}
}
} }
/** /**