How do I use JQuery's hasData () correctly?

I have a basic login-only web application where a button is clicked, a GET request is sent to the Foursquare API and then AJAX stores some response request data in a div like this:

checkinsCount[i] = data.response.venues[i].stats.checkinsCount;
$("#fs_results").data("checkinsCount", checkinsCount);
d = $("#fs_results").data("checkinsCount");
console.log(d);

      

Then I have a checkbox where I want to display the data in a div when the checkbox is clicked, but ONLY after checking that the data already exists.

So, I'm trying to use jQuery.hasData () to check this:

$('#checkbox1').click(function () {
if($.hasData($('#fs_results'))){
  $("#fs_results").toggle(this.checked);
} else{
  alert("false");
  $('#checkbox1').attr('checked', false);

}
});

      

But right now, I just get a false warning again, even when I checked the data. I am lost since this is my first time using jQuery / AJAX ... any help or suggestions?

+3


source to share


1 answer


hasData works on one element, which I believe (see docs ) you are passing the result of the request.

Thus, replacing

if($.hasData($('#fs_results'))){



from

if($.hasData($('#fs_results')[0])){

should solve the problem. In this case, you should use prop()

instead attr()

.

+2


source







All Articles