john
john

Reputation: 801

jQuery - get all links where text=mytext

I want to select all a elements that have text=mytext. I looking for something like that:

$('.scroll').find("a[text=" + "mytext" + "]").css('background-color', 'green');

Why this is not working?

edit:

<a href='http://stackoverflow.com'>stackoverflow</a>

text=stackoverflow

Upvotes: 0

Views: 135

Answers (3)

Use :contains()

$('.scroll').find("a:contains('mytext')").css('background-color', 'green');

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388316

in that case

$('.scroll a').filter(function(){
    return $.trim($(this).text()) == 'mytext'
}).css('background-color', 'green');

:contains() does not suit your case because it does not test for equality

Upvotes: 3

412
412

Reputation: 1428

You want to use :contains():

$('.scroll').find("a:contains('mytext')").css('background-color', 'green');

Upvotes: 1

Related Questions