Possible Duplicate:
Select element based on EXACT text contents
How do I select a span containing an exact text value, using jquery?
<div>
<span>find me</span>
<span>dont find me</span>
<span>xfind mex</span>
<span>_find me_</span>
</div>
To select a span containing a specific text value using jQuery, we can select all the spans and then use the filter method to find the one with the given text value. We call $(“span”) to get all the spans. Then we spread the spans into an array with the spread operator.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
You could use $('. gettext'). text(); in jQuery.
$('span')
.filter(function(){ return $(this).text() == 'find me'; })
.css('color','red');
.filter
allows you to apply logic to the elements and return only those that match your test(s) back.
In case you want a better integrated version, here's a :text()
selector:
(function($){
$.expr[':'].text = function(obj, index, meta, stack){
return ($(obj).text() === meta[3])
};
})(jQuery);
$('span:text("find me")').css('color','red');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With