Skip to content

Instantly share code, notes, and snippets.

@kshoufer
Last active November 2, 2015 08:57
Show Gist options
  • Save kshoufer/9263239 to your computer and use it in GitHub Desktop.
Save kshoufer/9263239 to your computer and use it in GitHub Desktop.
Ajax Live Search jQuery.
$('#search').keyup(function () {
var searchField = $('#search').val();
if (searchField.length)
{
var myExp = new RegExp(searchField, "i");
var found = 0;
$.getJSON('data.json', function (data) {
var output = '<ul class="searchresults">';
$.each(data, function(key, val) {
if (val.title.search(myExp) !== -1) {
console.log(val);
found = 1;
output += '<li>';
output += '<h2>' + val.title + '</h2>';
output += "<a href=" + val.website + ' target="_blank" >website</a>';
output += '</li>';
}
});
output += '</ul>';
if (found==1) {
$('#update').removeClass('update-hidden');
$('#update').html(output);
}
else {
$('#update').addClass('update-hidden');
}
});
} else {
$('#update').addClass('update-hidden');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment