Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery find anchor tag with exact href

I'm using the following code to find anchor tags with a matching href URL. Currently it will return anchors linking to the requested URL, eg. /images AND any links to subfolders of that url, eg. /images/recent. I would like it to only return the first if I'm only asking for /images.

$menuChildren = $menuChildren.has('a[href^="'+relativeUrl+'"],a[href^="/'+relativeUrl+'"],a[href^="'+url+'"]');
like image 286
jetlej Avatar asked Nov 07 '11 17:11

jetlej


2 Answers

You're using ^=, the Attribute Starts-With selector. As its name suggests, it matches attributes whose values start with the match string. Instead, use =, the Attribute Equals selector which requires an exact match:

$menuChildren = $menuChildren.has('a[href="'+url+'"]');
like image 173
meagar Avatar answered Nov 01 '22 19:11

meagar


If you want to match the href exactly then use the [href=...] version

$menuChildren = $('a[href="' + relativeUrl + '"]');
like image 15
JaredPar Avatar answered Nov 01 '22 20:11

JaredPar