Hi i am trying to perform a search on a json file called PCproducts.json which has in it various arrays with other data in it.I have seen various examples but seems that either i am doing something wrong in the code or calling the file in the wrong way.
This is my HTML form.
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Live Search</title>
<!-- <link rel="stylesheet" href="mystyle.css" /> -->
</head>
<body>
<div id="searcharea">
<label for="search">live search</label>
<p>Enter the name of the speaker</p>
<input type="search" name="search" id="search" placeholder="name" />
</div>
<div id="update"></div>
<script src="jquery.js"></script>
<script src="script3.js"></script>
</body>
</html>
This is my script.js file
$('#search').keyup(function() {
var searchField = $('#search').val();
var myExp = new RegExp(searchField, "i");
$.get('PCproducts.json', function(data) {
var output = '<ul class="searchresults">';
$(data).find("name").each(function(index, value){
var val = value.firstChild.nodeValue;
if ((val.search(myExp) != -1)) {
output += '<li>' + val + '</li>';
}
});
output += '</ul>';
$('#update').html(output);
}); //get
});
While this is part of the PCproducts.json file so as to get an idea
{
"pc":
[
{
"title":"Call of Duty - Ghosts",
"category":"PC",
"genre":"Action Multiplayer Game",
"developed":"Ubisoft",
"imgpath":"images/thumb/Call of Duty Ghosts(PC).jpg",
"released": "November 2013",
"price":"45.00 Eur",
"quantity":4
},
{
"title":"Assassins Creed IV - Black Flag",
"category":"PC",
"genre":"Action Adventure Game",
"developed":" Ubisoft",
"imgpath":"images/thumb/Assasins Creed Black Flag (PC).jpg",
"released": "October 2013",
"price":"45.00 Eur",
"quantity":4
},
],
"Wii":
[
{
"title":"Call of Duty - Black Ops",
"category":"Wii",
"genre": "Action Game",
"developed":"Ubisoft",
"imgpath":"images/thumb/Call of Duty Black Ops(Wii).jpg",
"released": "November 2013",
"price":"45.00 Eur",
"quantity":4
},
{
"title":"Fifa 14",
"category":"Wii",
"genre": "Football Game",
"developed":" EA Sports",
"imgpath":"images/thumb/Fifa 14(Wii).jpg",
"released": "October 2013",
"price":"45.00 Eur",
"quantity":4
},
],
"3DS":
[
{
"title":"Super Mario 3D Land",
"category":"3DS",
"genre": "Platform Game",
"developed":"Nintendo",
"imgpath":"images/thumb/Super Mario 3D Land(3DS).jpg",
"released": "November 2011",
"price":"35.00 Eur",
"quantity":4
},
{
"title":"Pokemon X",
"category":"3DS",
"genre": "Fantasy ,
"developed":" Nintendo/Game Freak",
"imgpath":"images/thumb/Pokemon X(3DS).jpg",
"released": "October 2013",
"price":"45.00 Eur",
"quantity":4
},
Can anyone maybe take a look at the above code and tell me what is wrong with the code or how to go around it, as i am really confused, as i manged to bring and display the above json file but got confused in the search function.