Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
How to create search not autocomplète using javascript to check if content exists or not in json file

I need help writing search input to check if a content exists or not in json file.

If exists show me a pop-up "alert" that says "exists" or a pop-up "alert" says not exists.

Example

enter image description here

HTML

<div class="search-bar">
  <input type="text" id="seeker">
</div>
<div id="content"></div>

JavaScript

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();
  });
});

JS FIddle: https://jsfiddle.net/abdelkaderft/o6kwrcjx/1/

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Consider the following example: https://jsfiddle.net/Twisty/ocbs9e8a/

JavaScript

jQuery(function($) {
  var data = [{
      "id": 198,
      "name": "Aaron Garo",
    },
    {
      "id": 345,
      "name": "Michael Stines",
    },
    {
      "id": 545,
      "name": "Ully Heiz",
    },
    {
      "id": 678,
      "name": "Asgaf Torino",
    }
  ];

  $.each(data, function(key, val) {
    var row = $("<div>", {
      class: "values"
    }).appendTo($('#content'));
    $("<h5>", {
      class: "value-id"
    }).html(val.id).appendTo(row);
    $("<p>", {
      class: "value-name"
    }).html(val.name).appendTo(row);
  });

  var $rows = $('.values');
  $('#seeker').keyup(function() {
    var term = $(this).val();
    // Showe all Rowa
    $rows.show();
    // Iterate each row
    $rows.each(function(i, el) {
      // Hide those that do not contain Term
      if ($(".value-id", el).text().indexOf(term) == -1) {
        $(el).hide();
      }
    })
  });
});

This is just one way to filter the items on display. For example, if the User enters 4, only two results are shown.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!