0

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

Demo

1
  • Initial state means "Weight" or "jHairline"? Commented Sep 27, 2016 at 8:00

1 Answer 1

2

Because you reset it to 0 and don't increment it on the next update. The quick fix would be

 $('#reset').click(function() {
    currentIndex = 0;

    $("h1").removeClass().addClass(weights[currentIndex]);
    $("h1").html(weights[currentIndex]);
    currentIndex=1;
  });

The proper way would be to do the incrementing at the beginning of the $('#text').on('click', function(e) { function

--Edit for clarification request--
This is how I would personally recommend writing it:

$(function () {
    var weights = ["jHairline", "jThin", "jLight", "jMedium"];
    var currentIndex = -1;
    $('#text').on('click', function (e) {
        currentIndex=(currentIndex+1)%weights.length;
        $("h1").removeClass().addClass(weights[currentIndex]);
        $("h1").html(weights[currentIndex]);

        e.preventDefault();
    });

    $('#reset').click(function () {
        currentIndex = 0;

        $("h1").removeClass().addClass(weights[currentIndex]);
        $("h1").html(weights[currentIndex]);
    });
});

The 2 lines I added/modified were var currentIndex = -1; and currentIndex=(currentIndex+1)%weights.length; .

So essentially, you are incrementing the number by 1 when the text.click function starts. If you start it at -1 at the beginning, then when text.click enters, it will increment it to 0. And setting it to 0 in reset will make sure it increments to 1 next time text.click is ran.

Further,

 if (currentIndex == weights.length - 1)
           currentIndex = 0;
        else
            currentIndex++;

is a bit more concise and clear when written as currentIndex=(currentIndex+1)%weights.length; . It is adding 1 to the number, and then modding (taking the remainder when dividing) to loop it back around to 0 once weight.length is hit.

Sign up to request clarification or add additional context in comments.

3 Comments

It worked thank you. You mentioned fixing that at the beginning. How can I do that?
Thank you very much. Its this kind of explanation that really helps non-coders like me to understand things properly + learn how to improve their code. Thumbs up!
Sure :-) glad it helped you out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.