Hello coders! I’m having some problem with the code below. I am trying to get an array from a JSON file, and add it in to a certain div as a list. But the problem is every time the list
is completed i see undefined
in the first line, then the other elements of the array are listed. Rest of the code seems working fine. BTW I know the code is a little weird but I just started learning JS .
function searchScript(){
var list;
console.log("The form was submitted");
$.getJSON('./data.json', function(data) {
var zz = document.getElementById("searchBox").value.toLowerCase();
var tt = data.camps[zz];
for (i = 0; i < tt.length ;i++) {
list +="<p><em>" + tt[i] + "</p></em>";
}
console.log(data.camps[zz].length);
document.getElementById("searchResults").innerHTML = list;
document.getElementById("searchBox").value = "";
});
}