jQuery Tutorials
jQuery Effects
jQuery HTML
jQuery Traversing
jQuery AJAX
jQuery Misc
Use jQuery to filter / search specific elements.
Do a casual search for items in the table:
Type something in the input field to search the table for first names, last names or emails:
Firstname | Lastname | |
---|---|---|
John | Doe | john@example.com |
Mary | Moe | mary@mail.com |
July | Dooley | july@greatstuff.com |
Anja | Ravendale | a_r@test.com |
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var
value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
Explained example: We use jQuery to open lines for each table to check if there are text values that match the input field value. The toggle()
method hides a row (display: none
) that does not match the search. We use the toLowerCase()
DOM method to convert text into lowercase, making the search character insensitive (allowing "john", "John", and "JOHN" to search).
Do a random search for items listed:
Type something in the input field to search the list for items:
Do a non-text search within the div element:
I am a paragraph.
Another paragraph.