Although there are some examples about this on the web, it does not seem to work correctly. I can\'t figure out the problem.
I have this simple html
For myself, using Jquery lib 2.1.1 the following did NOT work the way I expected:
Set element data attribute value:
$('.my-class').data('num', 'myValue');
console.log($('#myElem').data('num'));// as expected = 'myValue'
BUT the element itself remains without the attribute:
<div class="my-class"></div>
I needed the DOM updated so I could later do $('.my-class[data-num="myValue"]') //current length is 0
So I had to do
$('.my-class').attr('data-num', 'myValue');
To get the DOM to update:
<div class="my-class" data-num="myValue"></div>
Whether the attribute exists or not $.attr will overwrite.
THE ANSWER BELOW IS THE GOOD ONE
You aren't using the data method correctly. The correct code to update data is:
$('#foo').data('num', num);
So your example would be:
var num = $('#foo').data("num") + 1;
console.log(num)
$('#foo').data('num', num);
console.log(num)
Had similar problem and in the end I had to set both
obj.attr('data-myvar','myval')
and
obj.data('myvar','myval')
And after this
obj.data('myvar') == obj.attr('data-myvar')
Hope this helps.
Use that instead, if you wish to change the attribute data-num of node element, not of data object:
DEMO
$('#changeData').click(function (e) {
e.preventDefault();
var num = +$('#foo').attr("data-num");
console.log(num);
num = num + 1;
console.log(num);
$('#foo').attr('data-num', num);
});
PS: but you should use the data() object in virtually all cases, but not all...
I had the same problem of the html data tag not updating when i was using jquery But changing the code that does the actual work from jquery to javascript worked.
Try using this when the button is clicked: (Note that the main code is Javascripts setAttribute() function.)
function increment(q) {
//increment current num by adding with 1
q = q+1;
//change data attribute using JS setAttribute function
div.setAttribute('data-num',q);
//refresh data-num value (get data-num value again using JS getAttribute function)
num = parseInt(div.getAttribute('data-num'));
//show values
console.log("(After Increment) Current Num: "+num);
}
//get variables, set as global vars
var div = document.getElementById('foo');
var num = parseInt(div.getAttribute('data-num'));
//increment values using click
$("#changeData").on('click',function(){
//pass current data-num as parameter
increment(num);
});
If we wanted to retrieve or update these attributes using existing, native JavaScript, then we can do so using the getAttribute
and setAttribute
methods as shown below:
JavaScript
<script>
// 'Getting' data-attributes using getAttribute
var plant = document.getElementById('strawberry-plant');
var fruitCount = plant.getAttribute('data-fruit'); // fruitCount = '12'
// 'Setting' data-attributes using setAttribute
plant.setAttribute('data-fruit','7'); // Pesky birds
</script>
Through jQuery
// Fetching data
var fruitCount = $(this).data('fruit');
// Above does not work in firefox. So use below to get attribute value.
var fruitCount = $(this).attr('data-fruit');
// Assigning data
$(this).data('fruit','7');
// But when you get the value again, it will return old value.
// You have to set it as below to update value. Then you will get updated value.
$(this).attr('data-fruit','7');
Read this documentation for vanilla js or this documentation for jquery