I'm currently facing an issue with my search functionality. I have successfully loaded data from a JSON file, but the search feature is not working as expected. I've reviewed my code multiple times and can't identify any mistakes. I believe there might be an error in the following line of code: if (key.search(myExp) != -1)
$(function() {
var searchField = document.getElementById("search");
var fieldVal = searchField.value;
var myExp = new RegExp(fieldVal, 'i');
search.onkeyup = function() {
var request;
request = new XMLHttpRequest();
request.open("GET", "data.json");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
var info = JSON.parse(request.responseText);
var output = "";
for (var i = 0; i <= info.links.length; i++) {
for (key in info.links[i]) {
if (info.links[i].hasOwnProperty(key)) {
if (key.search(myExp) != -1) {
output += "<p>" + key + "</p>";
}
}
}
}
var update = document.getElementById("result");
update.innerHTML = output;
}
};
request.send();
};
});
json file:
{
"full_name" : "dima",
"links" : [
{ "blog" : "http://iviewsource.com" },
{ "facebook" : "http://facebook.com/iviewsource" },
{ "podcast" : "http://feeds.feedburner.com/authoredcontent" },
{ "twitter" : "http://twitter.com/planetoftheweb" },
{ "youtube" : "http://www.youtube.com/planetoftheweb" }
]
}