Create Jquery array from ajax response -
i trying add or remove classes divs. these divs should assigned different classes depending on value of html. i'm not sure i'm doing wrong code isn't working.
this code:
$.ajax({ type: 'post', url: 'loc/bcheck.php', success: function(data){ tarrx = new array(data); }, complete: function(){ $('.bhr').each(function(){ curelm = $(this); var btm = curelm.html(); if ($.inarray(btm, tarrx) !== -1){ curelm.addclass('disabled'); curelm.removeclass('available'); } else{ curelm.addclass('available'); curelm.removeclass('disabled'); } }); } }); <div class="bhr">1</div> <div class="bhr">5</div>
all values of divs' html caught correctly. , function runs without errors. allways same result, value not present in array.
'data' 1", "2", "3", "4 , using console.log returns ["1", "2", "3", "4"]
i think problem new array(data)
, if data of string type creates array 1 element string, else if data array tarrx
array 1 element array $.inarray()
return -1
.
$.ajax({ type: 'post', url: 'loc/bcheck.php', //set datatype json datatype: 'json', success: function (data) { var tarrx = data; $('.bhr').each(function () { var curelm = $(this); //wrong variable name& might have trim html contents var btm = curelm.html().trim(); if ($.inarray(btm, tarrx) !== -1) { curelm.addclass('disabled'); curelm.removeclass('available'); } else { curelm.addclass('available'); curelm.removeclass('disabled'); } }); } });
Comments
Post a Comment