How to search data in a JSON with AJAX? I want to do the search in a JSON field called tag and show another field (title and url) in a div.
HTML:
<div class="Minimizado top-menu-invisible hidden-mobile hidden-md hidden-sm">
<input id="searchterm" name="searchterm" />
<button id="search">Buscar</button>
<div id="results">
</div>
</div>
JS:
<script>
$("#searchterm").keyup(function(e) {
var q = $("#searchterm").val();
$.getJSON("userControls/HelpPaginas.json",
function(data){
$("#results").empty();
$.each(data, function(i, item) {
$("#results").append("<div>" + item.title + "<br><br></div>");
});
}
);
});
</script>
JSON:
[{
"Tag": "Toyota, Ford, Carro2, teste",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "Ford, chevy, opala, teste2",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "BMW, Audi, importado",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "Benz, Teste3, cars",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "GMC, Chevrolet, chevy, GM",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}, {
"Tag": "HUMMER, 4x4, teste4, agora vai, teste com tag",
"Title": "Titulo da página, teste",
"Url": "page1.aspx"
}]
But the worst error happens! not the error in the browser, it just does not search. It even makes the correct call in JSON but does not display in DIV
New JS:
$.getJSON("../userControls/HelpPaginas.json", function (data) {
var html = [];
/* loop through array */
$.each(data, function (index, d) {
html.push("title: ", d.title, ", ",
"tag: ", d.Tag, ", ",
"url: ", d.Url, "<br>");
});
$("#results").html(html.join('')).css("background-color", "orange");
});