javascript - get wrong value in data attribute jquery -


i have div data attribut like

<div class='p1' data-location='1'></div> 

and have script like

$('button').click(function(){      var loc = $('.p1').data('location');     alert('data location is'+loc);//show data      var num = 10;     var count = loc;     var element = $('.p1');     var intv = setinterval(anim,1000);      function anim(){         count++;         num--;         if(count==37){count = 1;}         if(num==1){clearinterval(intv);}         $(element).animatecss('bounceout',{             callback: function(){                 $(element).attr('data-location',count);                 $(element).animatecss('bouncein');             }         });      }     anim();  }); 

with script above data-location attribute updated 10, if click button again, data-location still 1

the first time use .data() access data-* attribute, value of attribute cached internally jquery, , .data() uses cache on. updating attribute .attr() not update cache, need use .data() update it. that's why need use

$(element).data('location', count); 

to update it.