Saturday, February 22, 2014

Live Search with jQuery & JSON

Meanwhile I was amazed with the power of jQuery for handling the JSON files. Then i decided to make a live search with JSON & jQuery so thats why i am here And now stop the BUK BUK and get into the code
Live search
in live demo search for javascript, husain, hunk, youtube, jquery

Download Script View Demo 

html

html is super easy it has 2 divs first one with the id of searcharea and second one with id of update. In search are Their is a input field and in update div we are going to show the result.
<div id="searcharea">
 <h2>Live Search</h2>
 <input type="search" id="search"/>
</div>
<div id="update"></div>

hunklessons.json(JSON file)

json is filled with some dummy data of hunklessons blog post
[
 {
  "name":"husain saify",
  "photo":"http://lh5.googleusercontent.com/-oqoAWmCo7CA/AAAAAAAAAAI/AAAAAAAAAPk/osoYaXlwlxI/s512-c/photo.jpg"
 },
 {
  "name":"hunk lessons",
  "photo":"http://3.bp.blogspot.com/-snGcDs0qmtY/Uv4F13kHtRI/AAAAAAAAAMk/x20wmnG6j_I/s1600/hunlessonslogo.png"
 },
 {
  "name":"Embed Youtube Videos With JavaScript",
  "photo":"http://2.bp.blogspot.com/-cNbriEi6Pio/UwXyIfzXQUI/AAAAAAAAAOA/l9rAlFgah5k/s1600/embed-yt-videos.png"
 },
 {
  "name":"Drag and Drop Game With html5 & javascript",
  "photo":"http://4.bp.blogspot.com/-NDMehgFOvWM/UwNMEcA1XLI/AAAAAAAAANk/c__iisocakg/s1600/dragndropgame.png"
 },
 {
  "name":"Css3 Progress Bar",
  "photo":"http://2.bp.blogspot.com/--J0-soRNh3M/UwC64Y6ttnI/AAAAAAAAANQ/JEu1I2zJTpE/s1600/cssprogressbar.png"
 },
 {
  "name":"jQuery Username Availability check",
  "photo":"http://1.bp.blogspot.com/-m4x9VmOI-2E/Uv5PtDXZlnI/AAAAAAAAAMw/QiAqCH-lNr8/s1600/username+available.png"
 },
 {
  "name":"Select And Upload Multiple Images With jQuery & Ajax",
  "photo":"http://3.bp.blogspot.com/-LVKfO3xavWU/UwBgusO2WuI/AAAAAAAAANA/kpzTqM4l0E8/s1600/selectanduploadmultipleimaes.png"
 },
 {
  "name":"hunk husain",
  "photo":"http://lh5.googleusercontent.com/-oqoAWmCo7CA/AAAAAAAAAAI/AAAAAAAAAPk/osoYaXlwlxI/s512-c/photo.jpg"
 }
]

JavaScript

In this file we are getting the hunklessons.json file with jquery $.getJSON('hunklessons.json', function(data){}); and looping throw the data with jQuery $.each(data, function(key, val));  and appending the data to a output variable at the last display the output variable.
<script type="text/javascript" src='//cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
<script type="text/javascript">
$('#search').keyup(function() {//when key is pressed in search bar
 var searchTerm = $(this).val(); //val of search bar
 var myExp = new RegExp(searchTerm, "i"); //regular experation

 $.getJSON('hunklessons.json', function(data){//get the json file

  var output = "<ul id='result'>";
  $.each(data, function(key, val){
   if(val.name.search(myExp) != -1){//search for the data in the json file
    output += '<li>';
    output += '<h3>' +val.name+ '</h3>';
    output += '<img src="'+val.photo+'" style="height:100px;">';
    output += '</li>';
   }
  });//end each
  output += "</ul>";
  $('#update').html(output);//output result to the update div
 });
});
</script>

Like Us on Facebook / Add Hunk As Friend