Auto filter out non-matching results using jQuery

sdfgg45

I want to make a filtersearch that filter out the divs not containing a h3 that matches the users input.

My html code so far:

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-info">
          <div class="panel-heading">
            <span class="panel-title text-info">
              <i class="fa fa-search"></i> Filter projects
            </span>
          </div>
          <div class="panel-body">
                <div class="col-md-12">
                    <input id="sok" class="form-control" placeholder="Filtrer on projects" type="text">
                </div>  
          </div>
        </div>
        <br>
    </div>
    <script>
        $('#sok').keyup(function () {
            var txt = $('#sok').val();
           if ($('.filter >  h3:contains('+txt+')',this).length  === 0) {
                    $(this).css("display","none");
            }
        });         
    </script>
</div>

This a contains one div holding a h3 that needs to be searchable,and if the searchinput dont match the contents of h3, the a-tag should be set to display-none.

<a href="/project-details/345" class="project">
    <div class="col-md-2 filter">
        <div class="panel panel-info panel-border top">
            <div class="panel-body project-box">
                <h3>Text title to be matches</h3>
                <p>content here</p>
            </div>
            <div class="panel-footer">
                <p>15-12-45</p>
            </div>
        </div>
    </div>
</a>

I don't know the best practice on how to do this, but any pointers will be appreciated.

rrk

Your selector is wrong. Select parent with .filter using closest() and made use of filter() function of jQuery.

  $('.filter h3').filter(function(){
      return this.innerHTML.toLowerCase().indexOf(txt) == -1;
  }).closest('.filter').hide();
  $('.filter h3').filter(function(){
      return this.innerHTML.toLowerCase().indexOf(txt) != -1;
  }).closest('.filter').show();

The above code will hide the elements which does not have the text and show ones with text.

See the demo below.

$('#sok').keyup(function() {
  var txt = $('#sok').val().toLowerCase();
  $('.filter h3').filter(function(){
      return this.innerHTML.toLowerCase().indexOf(txt) == -1;
  }).closest('.filter').hide();
  $('.filter h3').filter(function(){
      return this.innerHTML.toLowerCase().indexOf(txt) != -1;
  }).closest('.filter').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="row">
  <div class="col-md-12">
    <div class="panel panel-info">
      <div class="panel-heading">
        <span class="panel-title text-info">
                  <i class="fa fa-search"></i> Filter projects
                </span>
      </div>
      <div class="panel-body">
        <div class="col-md-12">
          <input id="sok" class="form-control" placeholder="Filtrer on projects" type="text">
        </div>
      </div>
    </div>
    <br>
  </div>
</div>
<a href="/project-details/345" class="project">
  <div class="col-md-2 filter">
    <div class="panel panel-info panel-border top">
      <div class="panel-body project-box">
        <h3>It</h3>
        <p>content here</p>
      </div>
      <div class="panel-footer">
        <p>15-12-45</p>
      </div>
    </div>
  </div>
</a>
<a href="/project-details/345" class="project">
  <div class="col-md-2 filter">
    <div class="panel panel-info panel-border top">
      <div class="panel-body project-box">
        <h3>xx</h3>
        <p>content here</p>
      </div>
      <div class="panel-footer">
        <p>15-12-45</p>
      </div>
    </div>
  </div>
</a>
<a href="/project-details/345" class="project">
  <div class="col-md-2 filter">
    <div class="panel panel-info panel-border top">
      <div class="panel-body project-box">
        <h3>yy</h3>
        <p>content here</p>
      </div>
      <div class="panel-footer">
        <p>15-12-45</p>
      </div>
    </div>
  </div>
</a>
<a href="/project-details/345" class="project">
  <div class="col-md-2 filter">
    <div class="panel panel-info panel-border top">
      <div class="panel-body project-box">
        <h3>Text title to be matches</h3>
        <p>content here</p>
      </div>
      <div class="panel-footer">
        <p>15-12-45</p>
      </div>
    </div>
  </div>
</a>
<a href="/project-details/345" class="project">
  <div class="col-md-2 filter">
    <div class="panel panel-info panel-border top">
      <div class="panel-body project-box">
        <h3>yy</h3>
        <p>content here</p>
      </div>
      <div class="panel-footer">
        <p>15-12-45</p>
      </div>
    </div>
  </div>
</a>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to filter results using jQuery search string

From Dev

Using Javascript/JQuery to Filter XML results

From Dev

How to use jQuery or JavaScript to filter out results above a range

From Dev

How to use jQuery or JavaScript to filter out results above a range

From Dev

How to filter out results based off of an Array with jQuery

From Dev

Using ElasticSearch can I filter out results based on the relevance?

From Dev

Checkboxes to filter results in jQuery

From Dev

Filter results of dropdownlist per prior dropdownlist selection using jquery

From Dev

jQuery filtering results - stange matching

From Dev

Using jquery to filter out duplicate data in a json populated select box

From Dev

Filter out JSON for only columns I need using jQuery/javascript

From Dev

angularjs filter by an array return all matching results

From Dev

Search filter function with non Latin characters using jquery

From Dev

Matching using labels return no results

From Dev

Filter OUT matching documents in elasticsearch with aggregation

From Dev

Filter OUT matching documents in elasticsearch with aggregation

From Dev

Camel route to filter out the auto response emails

From Dev

Camel route to filter out the auto response emails

From Dev

awk: is there a way to filter non-matching regex?

From Dev

awk: is there a way to filter non-matching regex?

From Dev

Select both matching and non-matching results in SQL query / subquery

From Dev

Using pattern matching to filter array

From Dev

Using pattern matching to filter array

From Dev

Non greedy matching using ? with grep

From Dev

bigquery - filter out unique results only

From Dev

bigquery - filter out unique results only

From Dev

Filter out TZInfo:Timezone Ruby results to exclude

From Dev

How do I filter out non-int values from a double array using stream?

From Dev

Filter out and kill non-docker processes

Related Related

  1. 1

    How to filter results using jQuery search string

  2. 2

    Using Javascript/JQuery to Filter XML results

  3. 3

    How to use jQuery or JavaScript to filter out results above a range

  4. 4

    How to use jQuery or JavaScript to filter out results above a range

  5. 5

    How to filter out results based off of an Array with jQuery

  6. 6

    Using ElasticSearch can I filter out results based on the relevance?

  7. 7

    Checkboxes to filter results in jQuery

  8. 8

    Filter results of dropdownlist per prior dropdownlist selection using jquery

  9. 9

    jQuery filtering results - stange matching

  10. 10

    Using jquery to filter out duplicate data in a json populated select box

  11. 11

    Filter out JSON for only columns I need using jQuery/javascript

  12. 12

    angularjs filter by an array return all matching results

  13. 13

    Search filter function with non Latin characters using jquery

  14. 14

    Matching using labels return no results

  15. 15

    Filter OUT matching documents in elasticsearch with aggregation

  16. 16

    Filter OUT matching documents in elasticsearch with aggregation

  17. 17

    Camel route to filter out the auto response emails

  18. 18

    Camel route to filter out the auto response emails

  19. 19

    awk: is there a way to filter non-matching regex?

  20. 20

    awk: is there a way to filter non-matching regex?

  21. 21

    Select both matching and non-matching results in SQL query / subquery

  22. 22

    Using pattern matching to filter array

  23. 23

    Using pattern matching to filter array

  24. 24

    Non greedy matching using ? with grep

  25. 25

    bigquery - filter out unique results only

  26. 26

    bigquery - filter out unique results only

  27. 27

    Filter out TZInfo:Timezone Ruby results to exclude

  28. 28

    How do I filter out non-int values from a double array using stream?

  29. 29

    Filter out and kill non-docker processes

HotTag

Archive