NiallMitch14 NiallMitch14 - 2 months ago 13x
Javascript Question

JQuery Unrecognized expression contains selector

I'm copying some code over from a project that used jQuery v1.11.2 to a new project which uses v3.1.0 and the following line of code doesn't work. I get an unrecognized expression error in the console:

$('#createEditTabs a[data-target=#tabEditConfig]').tab('show');

This line worked fine in the old project but fails to work in the new updated one. Not sure exactly why or if v3.1.0 has some differences when using contains selectors.


Wrap attribute value with quotes to avoid the issue with # in the beginning.

$('#createEditTabs a[data-target="#tabEditConfig"]').tab('show');
//                              -^--          --^-

The attribute value should be a valid identifier or quoted string. In your code, it's not a valid identifier(contains #) so use quoted string.