Loop function executed onclick JavaScript

Hi guys i’m trying to create a loop function so that each time the button is clicked the content will change. Where am I going wrong? right now it’s going to changeTestimonial2 when I click the button (the last in the loop)

function arrayOfTestimonials() {
    var arrayOfFunctions = [];
    arrayOfFunctions.push(changeTestimonial);
    arrayOfFunctions.push(changeTestimonial1);
    arrayOfFunctions.push(changeTestimonial2);

    for (var i = 0; i < arrayOfFunctions.length; ++i) {
        arrayOfFunctions[i](); // run your function
    }
}

16 thoughts on “Loop function executed onclick JavaScript”

Leave a Comment