<div>
<a>
Text1
<img alt="" stc="" />
</a>
<a>
Text2
</a>
</div>
I want to select all anchor elements that have text=text2
. I'm looking for something like this:
$('a[text=Text2]')
Edit: Why this is not working? For some some reason, it needs to be in this format:
$('div').find('a').find(':contains("Text2")')
You're looking for contains:
Use the
:contains()
filter.http://api.jquery.com/contains-selector/
You ask why this doesn't work:
The reason is,
.find()
will search children elements, you want.filter()
(because you already selected thea
- or you add the:contains
to the a find:As an additional note, rather than scanning for exact text inside a link it might be better to be scanning for attributes, e.g.
Now you can (accurately) scan for:
(I'm a big fan of the data-* attributes.)