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
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With