Search code examples
jquerycontains

jQuery - contains() - search only full match variables


Can I get using jQuery contains() only variables which is 100% matchable? For example, I have something like that:

<div>Test</div>
<div>Tester</div>

and when I use:

 $('div:contains("Test")').length

I get two results, but I want get only result which is full matchable for string which I use inside contains() function. Is there any function similar to contains() but which get only full matchable results?

I know, I can do it in that way:

for(i = 0; i < obj.length; i++)
{
    if ( obj[i].innerText == 'Test' )
    {
        alert('Thats it! obj[i]');
    }
}

But problem is when I want to check if that searched div is visible. When I use:

obj[1].is(':visible');

I got:

TypeError: $(...)[1].is is not a function

But when I use:

 obj.is(':visible')

I got:

true

That's problem for me, because I need to check only div which contains: "Test" only.

Is it possible to search only full matchable results?

Thanks.


Solution

  • Unfortunately there isn't an "exact match" selector in jQuery. A compact way to express this is with filter:

    $('div').filter(function() {return $(this).text() === 'Test';}).is(':visible')