Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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