jQuery: Problem with id containing a period or asterisk

Today I was writing a code to get error text from spring generated span field using jQuery. Where I noticed that name is having a period and asterisk which is not directly supported by selectors.

<div id=”test1″><span id=”*.errors“>The information you entered is invalid. Try again.</span></div>

So in order to make them working we need to use escape character triple slash (\\\) before asterisk and period. Otherwise it will try to treat asterisk (*) as all elements and period (.) as class name. See below.

if($(‘#\\\*\\\.errors’).length > 0){
        var displayvalue=$(‘#\\\*\\\.errors’).html();

Leave a Reply

Your email address will not be published. Required fields are marked *