How to get the id of the error message (Validation Message) span?


I am working on to add the accessibility features in the application and would like to get the id of the error message span tag to hook it up with the textbox aria-describedby attribute.  Please see the screenshots below. How can I achieve this as the id of the error message is not exposed directly to be used as <txterror>.id?

Hello @Aditya Airen,

There may be better ways, but here's what I would do -

If you have just one such Input widget whose ValidationMessage widget Id you wish to retrieve, then you could just use - 

var x = document.getElementsByClassName('ValidationMessage')[0].id;

If you have several such Input widgets on your screen, then you loop through all elements:

var xlist = document.getElementsByClassName('ValidationMessage');
 for(i=0; i < xlist.length; i++)
    // do something here

Hope that's what you were looking for,



Thanks AJ. Your approach was really helpful. 

I was also trying to figure out approaches and stumbled across this pattern. It seems like the id of the Validation message is generated as ValidationMessage_<textboxId>. I also checked other applications I worked and the naming convention is same everywhere. 

You are welcome Aditya, yes the pattern is consistent but its likely you don't need the added check that the id ends with <textboxId>. Your call of course. Happy coding!



Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.