I have this code to search through my JSON data, but I
want when I search, I get a popup saying if it exists or not in my JSON file. How can I fix it?
I basically need an HTML search box to search through corresponding JSON data and return the result in a popup:
var data = [
{
"id":198,
"name":"Aaron Garo",
},
{
"id":345,
"name":"Michael Stines",
},
{
"id":545,
"name":"Ully Heiz",
},
{
"id":678,
"name":"Asgaf Torino",
}
]
output = "";
$.each(data, function(key, val){
output += "<div class='values'>";
output += '<h5 class="value-id">' + val.id + '</h5>';
output += '<p class="value-name">' + val.name + '</p>'
output += "</div>";
});
$('#content').html(output);
/* SEEKER FUNCTION */
if (!RegExp.escape) {
RegExp.escape = function (s) {
return s.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&")
};
}
jQuery(function(){
var $rows = $('.values');
$('#seeker').keyup(function () {
var regex = new RegExp(RegExp.escape($.trim(this.value).replace(/s+/g, ' ')), 'i')
$rows.hide().filter(function () {
var text = $(this).children(".value-name").text().replace(/s+/g, ' ');
return regex.test(text)
}).show();
});
});
.values{
background: #FFFFAA;
}
.search-bar{
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="search-bar">
<input type="text" id="seeker">
</div>
<div id="content"></div>
2
Answers
You may mean like this:
Precisely what you need