Why do I get this error and how can I test for it so it wont break, I tried checking for null but obviously that wont work, thanks.
Please don't advice to not write the ID like this as I know its wrong but it is a possibility.
var jsonTest = [
{
"myId": "''''''\"\"\"\"'''''''''''''\"#####$'''''",
}
];
alert(jsonTest[0].myId);
// Works - alerts the myId
$('#' + jsonTest[0].myId ).length;
// Error: Syntax error, unrecognized expression:
// #''''''""""'''''''''''''"#####$'''''
jQuery's uses this code to detect an id based selector :
characterEncoding = "(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+"
...
"ID": new RegExp( "^#(" + characterEncoding + ")" ),
This regex fails for "''''''\"\"\"\"'''''''''''''\"#####$'''''"
or more simply for "'"
.
The query engine is limited, which isn't very surprising for a so concise language and id validity rules so lax. it can't handle any valid id.
If you really need to be able to handle any kind of valid id, use
$(document.getElementById(jsonTest[0].myId))
In fact, you should never use $('#'+id)
as it simply adds a useless (and a little dangerous) layer of parsing for the same operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With