javascript - Error: Undefined data -
i have function fetch new ads in every 10 seconds, im getting error
function getnewads(){ var newer ; // fetches old value 'data-value' attribute var old_val = $("#new_data").data('value'); $.ajax({ type: "post", url: "users/process.php", data:{ getnewads: "getnewads" }, cache: false , datatype: 'json', async: false }).success(function(dat){ if(dat.status == 'success'){ $("#new_data").empty(); for(i = 0;i < dat.counts; i++){ newer = dat.counts + old_val; alert(old_val); /// problem att old_val alerts object object. if(newer > 0){ $("#new_data").html('<div class="added_ad">'+ newer +' new ads </div>'); // set 'data-value' attribute #new_data element $("#new_data").data('value',newer); $("#new_data").show(); }else{$("#new_data").hide();$("#new_data").data('value',0);} } } }); }
and html div
<div id="new_data" class="new_data"> </div>
i have tested works old_val
alerting object object
doing wrong ? thanks
if alert old_val
shows object object
if alert old_val.val()
shows undefined old_val
use .attr()
instead of .data()
there known issues incompatible browsers on .data()
change code to
var old_val = $("#new_data").attr('data-value');
to val object.
notice: .attr()
, set, if u want set val obj use
$("#new_data").attr('data-value',newvalue);
Comments
Post a Comment