Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any selector to do a perfect match against text?

Tags:

jquery

On jQuery, I'm looking to a similar selector to :contains but I need to do a perfect match against an element text. Is there any?

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

so If I look for text = 1 I would only get the first div

like image 343
StackOverflower Avatar asked May 30 '12 17:05

StackOverflower


1 Answers

You can make one:

$.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/

like image 163
Sampson Avatar answered Sep 16 '22 20:09

Sampson