Skip to content

Instantly share code, notes, and snippets.

@jennifert
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennifert/f7fe005c96c5a48a7506 to your computer and use it in GitHub Desktop.
Save jennifert/f7fe005c96c5a48a7506 to your computer and use it in GitHub Desktop.
This is the first custom function for our search. It just look sin the JSON file using jQuery getJson. The full javascript code is at: https://github.com/jennifert/jekyll-site/blob/master/js/main.js
/*start search function;*/
var getSearchResults = function(url) {
var count = 0;
var searchedFor = getParameterByName('searchbox'); /*get the query parameter from search box*/
var searchedForTest = searchedFor.toLowerCase();
$('#searchbox').val(searchedFor); /*update input field with what was searched for*/
$.getJSON('/search.json', function(data) {
$('div#results').append('<section class="col-xs-12 col-sm-6 col-md-12">');
$.each(data, function(key, val){
/*values to variable to use more than once.*/
var blogTitle = val.title;
var blogCategory = val.category;
var blogTags = val.tags;
var blogLink = val.href;
var blogDate = val.date;
var blogSummary = val.summary;
/*search array for*/
var testCategory = $.inArray(searchedForTest, blogCategory );
var testTags = $.inArray(searchedForTest, blogTags );
if ( (blogTitle.toLowerCase().indexOf(searchedForTest) > -1 ) || (testCategory > -1 ) || (testTags > -1 ) ) {
displyResult(blogTitle,blogCategory,blogTags,blogLink,blogDate,blogSummary);
count++;
} else {
/*result not found. Do NOT increment count here.*/
}
}); /*end for each*/
$('h1#searchHeader').after('<h2 class="lead"><strong class="text-danger">'+ count+'</strong> results were found for the search for <strong class="text-danger">'+ searchedFor+'</strong></h2>');
$('div#rbuildResults').append('</section>');
}); /*end getjson*/
};/*end get search results*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment