Skip to content

Instantly share code, notes, and snippets.

@dre3s
Last active January 12, 2016 17:50
Show Gist options
  • Select an option

  • Save dre3s/bd10a9fe6c43718a0470 to your computer and use it in GitHub Desktop.

Select an option

Save dre3s/bd10a9fe6c43718a0470 to your computer and use it in GitHub Desktop.
my.js.search.json

#Search on Json data

$('#search').keyup(function(){                                                             
  var searchField = $(this).val();                                                         
  var myExp = new RegExp( searchField, "i" );                                              
                                                                                           
  $.getJSON('data.json', function(data){                                                   
    var output = '<ul class="searchresults">';                                             
    $.each(data, function(key, val){                                                       
                                                                                           
      if(val.name.search(myExp) != -1 || val.bio.search(myExp) != -1){                     
        output += '<li>'  ;                                                                
        output += '<h2>'  + val.name  + '</h2>';                                           
        output += '<img src="images/' + val.shortname  + '_tn.jpg" alt="' + val.name  + '">';
        output += '<p>' + val.bio  + '</p>';                                               
        output += '</li>' ;                                                                
      }                                                                                    
    });                                                                                    
                                                                                           
    output += '</ul>';                                                                     
                                                                                           
    $('#update').html(output);                                                             
                                                                                           
  }); //get JSON                                                                           
                                                                                           
})    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment