Arnab
Arnab

Reputation: 2354

Finding number of links on page which link to same

This is a continuation of this post

I need to find the number of links on a page which link to 'www.google.com' and have class 'abc'

Thanks

Upvotes: 0

Views: 147

Answers (4)

ziotob
ziotob

Reputation: 71

Maybe using a custom filter? (also for future scopes):

$("a.abc").filter(function(){
    return $(this).attr("href") == "http://google.it";
}).length;

If the methods returns true the element is selected, otherwise not, simply and easy to use!

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

Use chaining of attribute-value selector with class selector:

$("a[href='http://www.google.com'].abc").length

Working Demo

Upvotes: 1

PeterKA
PeterKA

Reputation: 24638

Considering you've not specified if the link should have/be www.google.com the following will catch all links that have the string www.google.com and the a has class abc:

var nLinks = $('a[href*="www.google.com"].abc').length;

REF:

jQuery Attribute Contains API Docs

Upvotes: 0

James Donnelly
James Donnelly

Reputation: 128791

Personally I'd use:

$('a.abc[href$="www.google.com"], a.abc[href$="www.google.com/"]').length

This will match any a element with a class of "abc" which has a href ending in www.google.com or www.google.com/.

Upvotes: 2

Related Questions