Break For inside an IF

Asked

Viewed 148 times

0

I’m breaking my head an hour and I can’t understand why this BREAK is not working inside my IF:

var nextSlide = searchElement("#next-slide").addEventListener("click", function(){  
var tabs = searchAllElements(".tab-name");
breakme: for(var i = 0; i < tabs.length; i++){
    var currentTab = tabs[i];
    if(currentTab.classList.contains("active")){
        if(i == tabs.length -1){
            var nextTab = tabs[0];
            return nextTab.firstChild.click();
        }else{
            var nextTab = tabs[i + 1];
            return nextTab.firstChild.click();
        }
        break breakme;
    }
}
});

This function I created, looks for an element with the class active and executes the onclick of the next. The result is bizarre!! it keeps running the for and running other elements...

  • 2

    The break is inside a if that has return in if and else, soon the break nor is it ever executed. What does the code want to do? and why does it invoke the click of the other tabs ? The click of the other tabs is the same as this click function ? If yes, you can incur a kind of infinite loop

  • there are tabs, which when clicked, perform a function that displays the content. This function I posted, it’s like a Carousel that when clicked, runs the onclick from the next tab.

1 answer

0

Both ifs has a return inside, it makes it never arrive in the break. I suggest you remove both returns so the code will reach the break normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.