I’ve got this piece of code, to search the contents of HTML elements with the class “widget” and use the jQuery functions fadeIn() and fadeOut() to only show elements that match the search term. I don’t know how to assign the search function to, because I don’t assign it to the search box’s change event. What is the equal of onchange= in jQuery? My current code:
function search(text, elements) {
var term = text.value.toLowerCase();
var elms = $(elements);
var elm;
for (var i = elms.length; i > 0; i-=1){
elm = elms.get(i);
if (elm.text().toLowerCase().indexOf(term)>=0 ) {
elm.fadeOut(500);
} else {
elm.fadeIn(500);
}
}
}
And the failing inline script to trigger the onchange:
<script type="text/javascript">
$(document).ready(function() {
$("#search").change(function() {
search($("#search").text(),".widget");
});
});
I would use keyup() instead of change, Change only fires on blur.