tags:

views:

30

answers:

3

Hi ,

I have a quick question about jquery ..

I want to find < a > tag witch has some text.

For ex my HTML :

    <a href="#">A</a>
    <a href="#">B</a>
    <a href="#">C</a>
    <a href="#">D</a>

And I want to find for ex the tag what has text "A" .

PS: In my logic it is something like this

$("a").text("A").css("background", "yellow");

but It changes all tags text in "A" ..

Thanks !!!

+2  A: 

...

 $("a:contains('A')").css( "background", "yellow" );
Sarfraz
contains will not do an absolute match, I guess. What he wants is an exact match, `filter` will do better.
Teja Kantamneni
@Teja Kantamneni: Possibly that's what questioner meant but as you see he did not mention that :)
Sarfraz
+1  A: 
$("a:contains('A')")

http://api.jquery.com/contains-selector/

Sam Dark
+2  A: 

Probably best to use the filter function. When you want to do an exact match on text, the contains function, I think, rapidly becomes unusable. You end up changing your text to make your function work better. Filters can be as complex as you want, within reason. If your actual need is as simple as your example -- single letters -- then contains would probably work just fine. Expect it to break though if add a link with AA, AB, ...

$('a').filter( function() { return $(this).text() == 'A'; } )
      .css( "background", "yellow" );
tvanfosson