Is there any selector that fits the text perfectly?

In jQuery, I am looking for a similar selector :contains , but I need to make a perfect match with the text of the element. Whether there is a?

 <div>1</div> <div>12</div> <div>13</div> <div>135</div> 

So if I search for text = 1 , I would only get the first div

+6
source share
3 answers

This can be done like this:

 var str = "whatever"; $(".elements").filter(function() { return $(this).text() == str; }); 
+4
source

You can do the following:

 $.extend($.expr[":"], { exactly: function( element, index, details, collection ){ return $(element).text() === details[3]; } }); $("div:exactly(Foo)").addClass("red"); $("div:exactly(230)").addClass("blu"); 

Fiddle: http://jsfiddle.net/jonathansampson/YUQLF/

+11
source
 var search = '1'; $('div').filter(function() { return $(this).text() == search; }).css('background','#f00'); 

Demo

+2
source

Source: https://habr.com/ru/post/917036/


All Articles