Mikey1980
Mikey1980

Reputation: 1001

jQuery - Help with .find() and adding id to an anchor tag

I have a menu that uses SSIs, problem is the tag needs an id of "here" for CSS to apply background image to show the end user's location on the site.

So is there a way to grab the browser's current url (ie: mypage.html), use the .find() function select the anchor tag and add id="here"?

I can see this working in concept but no matter what I try I can't seem to get jQuery to do it.

Upvotes: 1

Views: 1976

Answers (2)

Daniel LeCheminant
Daniel LeCheminant

Reputation: 51081

Depending on if it's a relative URL or not, something like

$("#yourmenu")
.find("a[href$=" + window.location.pathname + "]:first").attr("id", "here")

may work. This looks for a tags that have an href that ends with the current pathname; that may not be restrictive enough for you.

Upvotes: 2

Jage
Jage

Reputation: 8086

Try something like this?

var loc = window.location.href;
$('a[href='+loc+']').attr('id','here');

Upvotes: 2

Related Questions