Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How do I find the span containing the text "FIND ME"

<div>    <span>FIND ME</span>    <span>dont find me</span> </div> 
like image 456
MedicineMan Avatar asked Feb 24 '12 02:02

MedicineMan


People also ask

How do you find the value of text in span?

Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants.

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 can get current span value in jQuery?

live('click',function(){ var parentDiv = $(this). closest('span'); alert(parentDiv); var curprice=parentDiv. find('span[id=span_view]'). val(); alert(curprice); });


1 Answers

http://api.jquery.com/contains-selector/

$("span:contains('FIND ME')") 

ETA:

The contains selector is nice, but filtering a list of spans if probably quicker: http://jsperf.com/jquery-contains-vs-filter

$("span").filter(function() { return ($(this).text().indexOf('FIND ME') > -1) }); -- anywhere match $("span").filter(function() { return ($(this).text() === 'FIND ME') }); -- exact match 
like image 109
Malk Avatar answered Oct 09 '22 23:10

Malk