javascript - jQuery Json filter with search input without using any filter plugin -


i able show json data in html view, how can filter json data list based on firstname type in search input box, please me new javascript & jquery.

html

<input type="search" name="search" id="search" value="" /> <div id="placeholder"></div> 

javascript/jquery

var data={"users":[         {             "firstname":"ray",             "lastname":"villalobos",             "joined": {                 "month":"january",                 "day":12,                 "year":2012             }         },         {             "firstname":"john",             "lastname":"jones",             "joined": {                 "month":"april",                 "day":28,                 "year":2010             }         } ]}  $(data.users).each(function() {     var output = "<ul><li>" + this.firstname + " " + this.lastname + "--" + this.joined.month+"</li></ul>";     $('#placeholder').append(output); }); 

here fiddle.

you can use following

var data = {      "users": [{          "firstname": "ray",              "lastname": "villalobos",              "joined": {              "month": "january",                  "day": 12,                  "year": 2012          }      }, {          "firstname": "john",              "lastname": "jones",              "joined": {              "month": "april",                  "day": 28,                  "year": 2010          }      }]  }    $(data.users).each(function () {      var output = "<ul><li>" + this.firstname + " " + this.lastname + "--" + this.joined.month + "</li></ul>";      $('#placeholder').append(output);  });  $('#search').change(function () {      var yourtext = $(this).val();      if (yourtext.length > 0) {          $("li:contains(" + yourtext + ")").addclass('notify');      }      else{          $("li:contains(" + yourtext + ")").removeclass('notify');      }  });
.notify{      border: 1px solid red;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="search" name="search" id="search" value="" />  <div id="placeholder"></div>

up dated

var data = {      "users": [{          "firstname": "ray",              "lastname": "villalobos",              "joined": {              "month": "january",                  "day": 12,                  "year": 2012          }      }, {          "firstname": "john",              "lastname": "jones",              "joined": {              "month": "april",                  "day": 28,                  "year": 2010          }      }]  }    $(data.users).each(function () {      var output = "<ul><li>" + this.firstname + " " + this.lastname + "--" + this.joined.month + "</li></ul>";      $('#placeholder').append(output);  });  $('#search').keyup(function () {      var yourtext = $(this).val();      if (yourtext.length > 0) {          $("li:not(:contains(" + yourtext + "))").hide();      }      else{                    $("li").show();      }  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="search" name="search" id="search" value="" />  <div id="placeholder"></div>

up dated 2

var data = {      "users": [{          "firstname": "ray",              "lastname": "villalobos",              "joined": {              "month": "january",                  "day": 12,                  "year": 2012          }      }, {          "firstname": "john",              "lastname": "jones",              "joined": {              "month": "april",                  "day": 28,                  "year": 2010          }      }]  }    $(data.users).each(function () {      var output = "<ul><li>" + this.firstname + " " + this.lastname + "--" + this.joined.month + "</li></ul>";      $('#placeholder').append(output);  });  $('#search').keyup(function () {      var yourtext = $(this).val();      if (yourtext.length > 0) {          var abc = $("li").filter(function () {              var str = $(this).text();              var re = new regexp(yourtext, "i");              var result = re.test(str);              if (!result) {                  return $(this);              }          }).hide();      } else {          $("li").show();      }  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="search" name="search" id="search" value="" />  <div id="placeholder"></div>


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -