Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live search on an Div with input filter

https://jsfiddle.net/Lh9efLxm/

I have some troubbles with a live search script.

I have some div as "rows" and some p as "columns" declared

The input fild as #filter should hide all "rows" (div) with no relevance

$('#results div').hide();

But it seems I have some miss understood.

Can some one help me? thx

like image 512
Korty Avatar asked Jun 24 '17 11:06

Korty


People also ask

How do I add a filter to my website search?

Filter Content Using URL Argument You can also filter content using the URL argument. Type the content category or genre name in the site URL to search the content. After typing the category name, genre name in the URL, you need to hit Enter.

How do I filter search in HTML?

Example. <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.."> Note: We use href="#" in this demo since we do not have a page to link it to. In real life this should be a real URL to a specific page.

How do you make a search bar functional?

In the HTML code of search bar, we gave the input an id=”searchbar” and onkeyup we called, the function “search_animal”. onkeyup calls the function every time a key is released on the keyboard. We first get our input using getElementById. Make sure to convert it to lower case to avoid case sensitivity while searching.


2 Answers

In your script, you hide and show All the #results div and not the specific one in the each loop. So change the selector to this.

Plus, you forgot to include jQuery in the fiddle.

    $("#filter").keyup(function() {

      // Retrieve the input field text and reset the count to zero
      var filter = $(this).val(),
        count = 0;

      // Loop through the comment list
      $('#results div').each(function() {


        // If the list item does not contain the text phrase fade it out
        if ($(this).text().search(new RegExp(filter, "i")) < 0) {
          $(this).hide();  // MY CHANGE

          // Show the list item if the phrase matches and increase the count by 1
        } else {
          $(this).show(); // MY CHANGE
          count++;
        }

      });

    });
.header {
  display: flex;
}

.header p {
  flex: 1;
  font-weight: bold;
}

.results {
  display: flex;
}

.results p {
  flex: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="filter" type="text">

<div id="header">
  <div class="header">
    <p>ID</p>
    <p>Manufacturer</p>
    <p>Type</p>
    <p>PS</p>
  </div>
</div>

<div id="results">
  <div class="results">
    <p>1</p>
    <p>Toyota</p>
    <p>C 200</p>
    <p>114</p>
  </div>
  <div class="results">
    <p>2</p>
    <p>Mercedes</p>
    <p>C 220</p>
    <p>144</p>
  </div>
</div>

JSFiddle

like image 82
Itay Ganor Avatar answered Sep 27 '22 20:09

Itay Ganor


You can search in div by using filter function.

  jQuery("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    jQuery("#myDIV *").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="myInput" type="text" placeholder="Search..">

<div id="myDIV">
  <p>I am a paragraph.</p>
  <div>I am a div element inside div.</div>
  <button>I am a button</button>
  <button>Another button</button>
  <p>Another paragraph.</p>
</div>

de here

like image 24
symi khan Avatar answered Sep 27 '22 21:09

symi khan