John Dors John Dors - 1 month ago 12
Javascript Question

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"

Dij Dij
Answer Source

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);