Last active
August 27, 2018 13:18
-
-
Save portapipe/7679405f7ba3256043af4fc2ac247013 to your computer and use it in GitHub Desktop.
Ricerca / filtro real-time in jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2 align="center">Ricerca real-time in jQuery</h2> | |
<br/> | |
<div class="row "> | |
<div class="panel-group"> | |
<input type="text" id="filtro" class="form-control" placeholder="Cerca qualcosa, ad esempio 'spaghetti'" /> | |
<br/> | |
<div class="contenuto"> | |
A Milano ci sono degli ottimi ristoranti! | |
</div> | |
<div class="contenuto"> | |
Nelle aree turistiche marittime ci sono spesso pessimi ristoranti, salvo eccezioni! | |
</div> | |
<div class="contenuto"> | |
In montagna è difficile mangiare a perdifiato: c'è poco ossigeno! | |
</div> | |
<div class="contenuto"> | |
Martedì ho mangiato spaghetti. | |
</div> | |
</div> | |
<!--/panel-group--> | |
</div> | |
<script> | |
//Questa dicitura serve a SOVRASCRIVERE la funzione :contains usata da jQuery (e visibile nella funzione poco sotto) affinché sia case-insensitive | |
jQuery.expr[':'].contains = function(a, i, m) { | |
return jQuery(a).text().toUpperCase() | |
.indexOf(m[3].toUpperCase()) >= 0; | |
}; | |
$(document).ready(function(){ | |
$("#filtro").bind('keyup',function(){ | |
$(".contenuto").each(function(){ | |
if($("#filtro").val().length >= 2 ){ | |
if( $(this).is(":contains('"+ $("#filtro").val().toLowerCase() + "')") ) | |
$(this).css('display',''); | |
else | |
$(this).css('display','none'); | |
}else{ | |
$(this).css('display',''); | |
} | |
}); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment