jquery - Search json Array for a value -


i have json array of values dynamically populated , returned via ajax call. json data can contain number of objects. example, ajax call returned array of gems ['ruby', 'emerald', 'topaz'].

on html page have text input somewhere on page user can add new gem. when user adds gem (via button click), want see if exists in json array. if user enters 'diamond' check box should checked.

my code:

  function addgem(data){ //data dynamically populated data ajax call                  var gem = $("#gem").val(); // text input value of textbox user can enter gem. checking json array values against value                 $("#button").click(function() { //user clicks button add gem input textbox                     $.each(data, function (item) {                         if(item === 'gem')                         {                             $("#oldform").prop("checked", false); // leave checkbox unchecked                         } else                             {                                 $("#oldform").prop("checked", true); //check check box                             }                            });                       });                       } 

how can execute search of json array? need create empty array first ie. data = [];? thanks,

i think wanted use gem variable instead of 'gem' string.

something like:

$("#button").click(function() {     $.each(data, function (item) {         $("#oldform").prop("checked", item === gem);     });   }); 

but consider using indexof:

$("#button").click(function() {     $("#oldform").prop("checked", data.indexof(gem)>-1); }); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -