Why am I required to click twice in order to change the classes after 'reseting'? How can I fix this issue?
The desired result is to bring the function back to the initial state and cycle through the arrays normally. Demo below.
$(function () {
var weights = ["jHairline", "jThin", "jLight", "jMedium"];
var currentIndex = 0;
$('#text').on('click', function (e) {
$("h1").removeClass().addClass(weights[currentIndex]);
$("h1").html(weights[currentIndex]);
if (currentIndex == weights.length - 1)
currentIndex = 0;
else
currentIndex++;
e.preventDefault();
});
$('#reset').click(function () {
currentIndex = 0;
$("h1").removeClass().addClass(weights[currentIndex]);
$("h1").html(weights[currentIndex]);
});
});