Reputation: 1440
I'm facing problem with getting updated value from data attribute!
Check here http://jsfiddle.net/Jim_Toth/ZLJbt/show/
Click on (How much?) and alert will get you the current value
But after you click on (Add One More?) will add +1 to current value
If you clicked on (How much?) again will get you the old value you got before
So how to get the updated value not the old I've gotten before?
check the code here http://jsfiddle.net/Jim_Toth/ZLJbt/
$('div').click(function () {
alert($(this).data('foo'));
});
var div = 1;
$('button').click(function () {
$('div').attr('data-foo', div);
div += 1;
});
Upvotes: 1
Views: 267
Reputation: 11812
Demo: http://jsfiddle.net/ZLJbt/2/
$('div').click(function () {
alert($(this).data('foo'));
});
$('button').click(function () {
var count = $('div').data('foo') || 0;
$('div').data('foo', count + 1) ;
});
Upvotes: 1
Reputation: 388316
jQuery .data() uses data-*
only to initialize the data values... there after if you want to modify then use .data() sto set the value
you should set .data() not attr()
var div = 1;
$('button').click(function () {
$('div').data('foo', div);
div += 1;
});
Demo: Fiddle
Upvotes: 5