JQuery - Syntax error

I use the following to identify the currently selected (active) link on the site:

 $(function(){
 var path = location.pathname.substring(1);
 if ( path )
 $('#sidebar_content a[@href$="' + path + '"]').attr('class', 'selected');
 });

It seems to identify the path correctly, but also generates an error

Error: uncaught exception: syntax error, unrecognized expression: [@href $ = "clinics / ohs_north_carolina"]

The source of the page does not show that the link has added the class.

Thank you for your help.

Thanks.

+3
source share
1 answer
Style selectors

[@attr]were removed in jQuery 1.3. Remove the character @and it should work.

$('#sidebar_content a[href$="' + path + '"]').attr('class', 'selected');

From docs :

. jQuery 1.3 [@attr] ( jQuery 1.2). "@" , .

+6

Source: https://habr.com/ru/post/1724415/


All Articles