mercredi 31 août 2016

Searching JSON with Jquery

I keep getting Uncaught TypeError: Cannot read property 'search' of undefined

  $('.searchBar').keydown(function(){
  $.getJSON('js/data.json', function(data){
    var search = $('.searchBar').val();
    var regex = new RegExp(search, 'i');
    $.each(data, function(key,val){
      if((val.id.search(regex) != -1) || (val.title.search(regex) != -1)){
        var img = "<img src= " + val.imgUrl + "/>"
        var title = "<h1>" + val.title + "</h1>";
        var description = "<p>" + val.description + "</p>";
        var cta = "<button><a href= " + val.ctaUrl + ">" + "</a>Find out more about this product</button>";
        $(img).appendTo('#data');
        $(title).appendTo('#data');
        $(description).appendTo('#data');
        $(cta).appendTo('#data');
      }
    });
  });
})




Aucun commentaire:

Enregistrer un commentaire