Jquery contains with class

I am using jQuery: contains a selector for filtering through link tags. The following code works great.

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

      

I would only like to run this function if the anchor tags are inside a specific class, I've tried the following but it doesn't work:

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

      

+2


a source to share


4 answers


None of these will work. You have brackets and wrong quotes all over the store.

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

      

and then



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

      

Which is working now: http://www.jsfiddle.net/muzQQ/

+2


a source


To select by class name, place period at the beginning:



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

      

+3


a source


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

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

      

+1


a source


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

      

if you need to get the above using jQuery use

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

      

+1


a source







All Articles