I have some code that has been working since it’s beginning, well over a two months. The selectors had dynamic content, stored in the variable newClasses. My code used to be $("'" + newClasses + "'"), and about a two days ago, after I added separate functionality on the page, not effecting this code, and not importing any libraries or anything else, I got the error of
Uncaught Error: Syntax error, unrecognized expression: '.fs.btn.heading.abstract' code.jquery.com/jquery.min.js:2
AFTER a day of troubleshooting, I found that if i removed the "'" + and + "'", and it became $(newClasses), the functionality started working again. FYI: the variable newClasses did not include 's on either end of the string.
Now, I know this is somewhat of a different question, but what code could be added in a separate section to effect the functionality, and result in my having to remove the concatenation of the single quotes?
Notes –
- I am not looking for code help to the webpage, more troubleshooting methodology and thinking answers.
- due to the volume of code, and the different code that was added, it is pointless to post the code, but you can feel free to review the Git code here
- I even tried reverting to a prior commit, and for some reason, it still had the same error.
- I have deduced from my programming sessions that when the console refers to JS or JQuery on a low level line number (2 in this case), it usually has something to do with a trivial
;,), or}, but JSLint didn’t show up that particular anomaly. - no browser change since then, but I did reboot, and potentially had system updates waiting to finish.
Is it possible that the API of jQuery changed? something else that I’m not thinking of? Your suggestions as to possible culprits is appreciated.
The selector is just a string. If your variable is already a string type, then you don’t need to add quotes to each end and doing so will probably cause an error as there are not supposed to be quotes on each end of the string. So,
$(var)should work just fine.Quotes are used to declare a string literal like:
but are not needed if you already have the data in a variable. So, if you have a variable called
selthat already contains the string#test, then you can just do:The only time you might add quotes into a selector string is if you need quotes embedded into the middle of the string such as this selector: