Add +1 for data attributes not working

This's my codes:

var following_tags = $('.follow-tags-start').attr("data-following-tags");

$('.follow-tags-start').attr('data-following-tags', following_tags+1);

the issue that if the count of data-following-tags="0" after do the action the count add 1 with the exists count so each time i do the action it's add 1 with 0. data-following-tags="01111"

2 answers

  • answered 2017-08-16 19:25 Dij

    you can use parseInt() (assuming these are integer numbers) to convert data attribute to integer, then you can add 1. something like this:

    var following_tags = parseInt($('.follow-tags-start').attr("data-following-tags"));
    
    $('.follow-tags-start').attr('data-following-tags', following_tags+1);
    

  • answered 2017-08-16 19:25 adeneo

    You can just use a callback, and coerce the value to a number

    $('.follow-tags-start').attr("data-following-tags", function(attr) {
        return (+attr) + 1
    });