Skip to content

Instantly share code, notes, and snippets.

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:
/*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 =;
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 ) ) {
} 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>');
}); /*end getjson*/
};/*end get search results*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment