How do I select a span containing an exact text value, using jquery?

15,907
$('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');
Share:
15,907
MedicineMan
Author by

MedicineMan

Java, .NET, and JavaScript developer in the SF Bay Area

Updated on July 13, 2022

Comments

  • MedicineMan
    MedicineMan over 1 year

    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>