Syntax error, unrecognized expression: Jquery



I want to add/remove the active class from the data-filer = variable, but this error gives: Uncaught Error: Syntax error, unrecognized expression: li[data-filter=.arroz-1]

$('li[data-filter=' + xNew + ']').addClass("active");
$('li[data-filter=' + xOld + ']').removeClass("active");

What am I doing wrong?

Answer

You missed some apostrophe. Try this:

$("li[data-filter='" + xNew + "']").addClass("active");
$("li[data-filter='" + xOld + "']").removeClass("active");


Source: stackoverflow