JQuery Selector error in element with dollar sign on its's id (Syntax error, unrecognized expression)

in #dotnet7 years ago

If you're using a .NET Web Forms an JQuery you may have this problem when you're trying to select an HTML element with a dollar sign on it (generated by the .net platform), something like this: $('#element$with$dollarSign').

This will generate an error:

This is because jQuery considers this an invalid selector, or more correctly said, a meta-character (!"#$%&'()*+,./:;<=>?@[]^`{|}~).

To successfully use this meta-charaters you will need to escape it with backslashes, like this $('#element\$with\$dollarSign')

So if you're on a project and are dealing with this issues before using a generated id you can escape the string before using: '#element$with$dollarSign'.replace(/[!"#$%&'()*+,./:;<=>?@[\]^`{|}~]/g, "\\$&")

You can check this issue with more detail on JQuery page: http://api.jquery.com/category/selectors/

Sort:  

Congratulations @coastpear! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!