Select link by text (exact match)

You can do this:

$('a').filter(function(index) { return $(this).text() === "This One"; });

Reference: http://api.jquery.com/filter/


A coworker of mine extended jQuery with a function to do this:

$.expr[':'].textEquals = function(a, i, m) {
    return $(a).text().match("^" + m[3] + "$");
};

The result is that you can select something by exact text this way:

$("label:textEquals('Exact Text to Match')");

This makes it easy, since you don't have to remember the exact syntax each time. His entire post is here: jQuery Custom Selector for selecting elements by exact text :textEquals


To expand on FishBasketGordo's answer. If you are trying to make the selection on a large amount of elements, use :contains() first to narrow down and then apply the filter.

This will improve the overall speed:

$('a:contains("This One")').filter(function(index)
{
    return $(this).text() === "This One";
});

had to modify Nariman's solution to be:

$.expr[':'].textEquals = function(a, i, m) {
    var match = $(a).text().match("^" + m[3] + "$")
    return match && match.length > 0;                                                                                                                                                                                                                                            
}

Otherwise didn't work on chrome (Linux)


I was using the extension

$.expr[':'].textEquals

But I have found that the implementation no longer works with jQuery 1.7 (apparently a change in Sizzla.filter). After struggling for some time to make it work I have simply written a jQuery plugin to accomplish the same.

$.fn.textEquals = function (text) {
    var match = false;
    $(this).each(function () {
        if ($(this).text().match("^" + escapeRegex(text) + "$")) {
            match = true;
            return false;
        }
    });
    return match;
};

Use:

$(".ui-autocomplete li").textEquals('Exact Text to Match');

Just wanted to share, in case someone else runs into this (,