:contains for multiple words

Posted by Emin on Stack Overflow See other posts from Stack Overflow or by Emin
Published on 2010-05-23T00:54:51Z Indexed on 2010/05/23 1:01 UTC
Read the original article Hit count: 323

Filed under:
|
|

I am using the following jQuery

        var etag='kate'

        if (etag.length > 0) {
            $('div').each(function () {
                $(this).find('ul:not(:contains(' + etag + '))').hide();
                $(this).find('ul:contains(' + etag + ')').show();
            });
        }?

towards the following HTML

  <div id="2">
<ul>
  <li>john</li>
  <li>jack</li>
</ul>
<ul>
  <li>kate</li>
  <li>clair</li>
</ul>
<ul>
  <li>hugo</li>
  <li>desmond</li>
</ul>  
<ul>
  <li>said</li>
  <li>jacob</li>
</ul>
  </div>

    <div id="3">
<ul>
  <li>jacob</li>
  <li>me</li>
</ul>
<ul>
  <li>desmond</li>
  <li>george</li>
</ul>
<ul>
  <li>allen</li>
  <li>kate</li>
</ul>  
<ul>
  <li>salkldf</li>
  <li>3kl44</li>
</ul>
  </div>

basically, as long as etag has one word, the code works perfectly and hides those elements who do not contain etag. My problem is, when etag is multiple words (and I don't have control over it. Its coming from a database and could be combination of multiple words seperated with space char) then the code does not work..

is there any way to achieve this?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about contains