/* JSON search v1.0 */ /* initiate variables * @jsonData used in function callback * @regex used in function useParsedData & defined onkeyup of search field * @searchField = the id of your search input field; the where people type to search for something */ var jsonData, regex, searchField = document.querySelector("#search"); /* * FUNCTION Callback * function to get JSON data * param @json = your JSON formatted data; the complete data set you want to search within */ function callback(json) { jsonData = json.page; // change "photo" to the 1st level of your json data -> see documentation for more examples useParsedData(json.page); } /** * FUNCTION useParsedData * parse JSON data; use data to create unordered list; insert search results in DOM * param @data = 1st level of your JSON formatted data */ function useParsedData(data) { var output = "'; document.querySelector("#update").innerHTML = output; //insert search results in element with id="update" } // when someone types something into your search field, look for that keyword/phrase in your JSON data set searchField.addEventListener("keyup", function() { var searchInput = searchField.value; regex = new RegExp(searchInput, "i"); // case insensitive! see http://www.w3schools.com/jsref/jsref_obj_regexp.asp for more info on regex useParsedData(jsonData); }); searchField.focus(); // put focus on search input field // create script with JSON data and append it to your HTML document head var script = document.createElement('script'); script.setAttribute("type", "text/javascript") script.setAttribute("src", "js/search.php"); document.getElementsByTagName("head")[0].appendChild(script);