Scooter Daraf
Scooter Daraf

Reputation: 535

Error: Undefined data

I have this function to fetch new ads in every 10 seconds, but im getting the error

function getnewads(){
    var newer ;
    // This fetches old value from '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>');
                    // This set 'data-value' attribute to #new_data element
                    $("#new_data").data('value',newer);
                    $("#new_data").show();
                }else{$("#new_data").hide();$("#new_data").data('value',0);}
            }
        }
    });
}

and this is my html div

 <div id="new_data" class="new_data">
 </div>

I have tested everything works just this old_val is alerting Object object What am I doing wrong ? thanks

If I alert old_val it shows Object object

If I alert old_val.val() its shows undefined old_val

Upvotes: 0

Views: 1064

Answers (1)

UnskilledFreak
UnskilledFreak

Reputation: 306

use .attr() instead of .data()

there are known issues with incompatible browsers on .data()

change your code to

var old_val = $("#new_data").attr('data-value');   

to get the val from the object.

Notice: .attr() is get and set, if u want to set a val to this obj use

$("#new_data").attr('data-value',newvalue);

Upvotes: 1

Related Questions