proee
proee

Reputation: 211

jquery contains with class

I'm using jQuery :contains selector to filter through some link tags. The following code works great.

$('a:contains("string")').each(function(i){
//do some stuff
});

I'd like only run this function if the anchor tags are inside a specific class, I've tried the following but it does not appear to work:

$('.my_class a:contains("string")').each(function(i){
//do some stuff
});

Upvotes: 2

Views: 3125

Answers (4)

hasitha
hasitha

Reputation: 11

<div class="GridCell">Click to review.</div>

if you need to get the above using jQuery, use

$('div.GridCell:contains(Click to review.)')

Upvotes: 1

Matt
Matt

Reputation: 75317

Neither of those will work. You've got brackets and mis matching quotes all over the shop.

$('a:contains(string)').each(function(i){
    //do some stuff
});

and then

$('.my_class a:contains(string)').each(function(i){
    //do some stuff
});

Which now works: http://www.jsfiddle.net/muzQQ/

Upvotes: 2

Justin Ethier
Justin Ethier

Reputation: 134157

Try this, with a dot at the start of the class name:

$('.my_class a:contains('string').each(function(i){
//do some stuff
});

Upvotes: 1

nickf
nickf

Reputation: 546045

To select by class name, put a dot at the start:

$('.my_class a:contains("string")')

Upvotes: 3

Related Questions