I am implementing a search box. One of the scenarios is that the total count of results must be announced. To achieve this I put the <p> tag inside of an aria-live region, and it announces it as expected.
Expected scenario:
User types a string --> hits enter --> results appear and string is announced.
The edge case is if the user hits enter twice.
If the user presses enter again without any changes, nothing is updated since the count is still the same and nothing is announced.
I tried using this on enter click:
if (document.getElementById("header")) {
const currentText: string = document.getElementById("header").innerHTML;
document.getElementById("search-header").innerHTML = "";
document.getElementById("search-header").innerHTML = currentText;
}
But, it still did not announce.
Is there another way to accomplish this?
I ran into a similar situation recently. You've already implemented half of the solution (resetting the
innerHTMLto an empty string, then setting it to thecurrentText). The other half of the solution is to set thearia-relevantattribute toall.By default the
aria-relevantattribute is set toadditions text, but that leaves out theremovedoption. Since you need the screen reader to pickup the change to an empty string (when you remove the text), you need to set the attribute toall(which is the same asadditions text removed).As a best practice the
aria-relevantattribute should normally be left alone, but I haven't found another way to get the screen reader to make the same announcement twice.In your case I would also add the
aria-liveproperty directly to the element with anid="search-header". It should look something like this:You can read more about the
aria-relevantattribute here.And, there's more information about
aria-liveregions here.