tags:

views:

64

answers:

3

I'd like to filter several SPANs with the text they contain:

$('spanFilter').filter(function(){
    var html = $(this).html();
    return //comparison html LIKE %asdf%
});

In the comparison, I'm trying to do something like MySQL's LIKE:

field LIKE %asdf%

How can I achieve that?

+1  A: 

Not sure exactly what it is you're after but you can use the :contains() selector. For example:

$('spanFilter').filter(function(){
    var html = $(this).html();
    $("span:contains(asdf)", this).addClass('highlight");
});
cletus
Did you meant not to use variable 'html'?
TStamper
Come to think of it, the html variable is unnecessary.
cletus
+2  A: 
field LIKE %asdf%

can work like

$("span:contains('asdf')")

:contains(text) - Matches elements which contain the given text.

TStamper
A: 

There are many ways to do this:

  1. Simple: using indexOf

    return html.indexOf('asdf') >= 0;

  2. Complex: use RegExp

    return /asdf/.test(html);

Note that RegExp are more powerful and can match different texts. Here's a guide about them.

Keeper