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
W3C has defined a new function for this this: CSS.escape(). In your example code, it would look like this:
var jsonTest = [
{
"myId": "''''''\"\"\"\"'''''''''''''\"#####$'''''",
}
];
$('#' + CSS.escape(jsonTest[0].myId)).length; // Works
Browsers don't yet support it, but there is a polyfill library that you can use in the mean time: https://github.com/mathiasbynens/CSS.escape
I've used it with success in my project.