I want to identify anchor tags that have an href containing a particular string and then add a class.
I thought the way to do this might be to use the :contains(text) filter but that appears not to work. I notice in the examples given in the jQuery documentation that the selector is only used to add css to the target element.
Can you not use this selector to add a class to something like an anchor tag?
My example case follows:
<script type="text/javascript">
$('a:contains("apple")').addclass("monkey");
</script>
Where the href in the anchor is http://www.apple.com
This code doesn’t work.
Alternatively what is the best way to accomplish my goal of finding anchors that contain particular strings and then adding a class or id to the anchor.
Thanks.
You want
$('a[href*="apple"]')instead.:contains()checks the inner text of the element.