2

I am using the following to identify the current selected (active) link in a site:

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

It appears to identify the path properly but also generates an error

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

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

Would appreciate some help.

Thanks.

1 Answer 1

6

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

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

From the docs:

Note: In jQuery 1.3 [@attr] style selectors were removed (they were previously deprecated in jQuery 1.2). Simply remove the '@' symbol from your selectors in order to make them work again.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Ayman, that certainly solved the problem. Thank you. One followup. Does this code apply the class="selected" attribute to the anchor tag? If so it does not show up in the source code. Thanks, Forrest
Hi fmz, it's better to use .addClass('selected') as it does not override existing classes. Also, JavaScript changes to the DOM tree are usually not visible in the "view source" window in browsers.
Hi Ayman, I appreciate your help here. I am trying to nail down a navigation issue that has been bugging me for days. I have an accordion navigation that I need to resolve. have it working on one level in that I can highlight the active link, but it is currently set manually. I would like to set that dynamically and this looks promising but appears to be lacking something. I would be happy to open another thread, but what I really need is an opportunity to work it through. Can you help?
Hi fmz, please open a new thread and provide details. Feel free to add a link here so that I can find it. I'm sure you will get help whether from me or someone else. :)
Hi Ayman, Here is the link: stackoverflow.com/questions/1829701/… Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.