Search code examples
javascriptjqueryfilterfilteringshow-hide

Filter by search using text found in element within each div


I found a fiddle that was useful for hiding text based on the text used in a search box, but have not been able to figure out how to adapt this method to a div with multiple elements in it. How could I change the jQuery in the attached fiddle to make it filter the div elements that match the search entered instead of the text found in the list items?

http://jsfiddle.net/point71echo/rof67uy6/2/

<input placeholder="Search Me" id="box" type="text" />

<ul class="navList">
    <li>apples</li>
    <li>apricots</li>
    <li>acai</li>
    <li>blueberry</li>
    <li>bananas</li>
    <li>cherry</li>
    <li>coconut</li>
    <li>donut</li>
    <li>durean</li>
</ul>


        <div class="connect-cat" catname="apples" style="visibility: visible; display: block;">
            <a href="/connect/apples">
                <div style="padding:5px;cursor:pointer;">
                    <div>
                        <img width="60" class="cat-logo" src="http://t2.gstatic.com/images?q=tbn:ANd9GcS0rYdTsHK-IwuWCNA_xXq44v76dFH2wPc5wdErF9yWHty-wqY4Bg" alt="apples">
                    </div>
                    <span>apples</span>
                </div>
            </a>
        </div>



        <div class="connect-cat" catname="apricots" style="visibility: visible; display: block;">
            <a href="/connect/apricots">
                <div style="padding:5px;cursor:pointer;">
                    <div>
                        <img width="60" class="cat-logo" src="http://t2.gstatic.com/images?q=tbn:ANd9GcS0rYdTsHK-IwuWCNA_xXq44v76dFH2wPc5wdErF9yWHty-wqY4Bg" alt="apricots">
                    </div>
                    <span>apricots</span>
                </div>
            </a>
        </div>

Here's the jQuery used:

$('#box').keyup(function(){
   var valThis = $(this).val().toLowerCase();
    if(valThis == ""){
        $('.navList > li').show();
    } else {
        $('.navList > li').each(function(){
            var text = $(this).text().toLowerCase();
            (text.indexOf(valThis) >= 0) ? $(this).show() : $(this).hide();
        });
   };
});

Solution

  • If I needed to add another text element to each div box, is there a way that all text in each div could be searchable? Here's the fiddle with one additional text element - jsfiddle.net/point71echo/mttgj1tt/1 – point71echo

    Here is a solution to search two elements at once in native JavaScript, without any libraries.

    document.getElementById("box").onkeyup=function(){
      var matcher = new RegExp(document.getElementById("box").value, "gi");
      for (var i=0;i<document.getElementsByClassName("connect-cat").length;i++) {
        if (
          matcher.test(document.getElementsByClassName("name")[i].innerHTML) || 
          matcher.test(document.getElementsByClassName("category")[i].innerHTML)
        ) {
          document.getElementsByClassName("connect-cat")[i].style.display="inline-block";
        } else {
          document.getElementsByClassName("connect-cat")[i].style.display="none";
        }
      }
    }
    

    JSFiddle

    If you want to use jQuery to make it more readable (those stupid long DOM functions):

    $("#box").on('keyup', function(){
      var matcher = new RegExp($(this).val(), 'gi');
      $('.connect-cat').show().not(function(){
          return matcher.test($(this).find('.name, .category').text())
      }).hide();
    });
    

    JSFiddle