Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 653
MedicineMan Avatar asked Feb 24 '12 02:02

MedicineMan


People also ask

How do I select a span containing a specific text value using jQuery?

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.

How do I select a specific tag in jQuery?

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.

How do I get just the text from HTML in jQuery?

You could use $('. gettext'). text(); in jQuery.


1 Answers

$('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');
like image 180
Brad Christie Avatar answered Oct 24 '22 18:10

Brad Christie