I think the other answers told you why tabs[tabNumber] does not work (because it is based on the evaluation of the for loop, and so is always a greater value tabNumber).
That's why I would recommend using a cycle .forEach . Beware, because it does not work with arrays of DOM nodes created document.querySelectorAll() , but you can use:
// ES6 Array.from(document.querySelectorAll('...')) // ES5 [].slice.call(document.querySelectorAll('...')) ) // ES6 Array.from(document.querySelectorAll('...')) // ES5 [].slice.call(document.querySelectorAll('...'))
In any case, I made a simplified working demonstration of your code. Please note that I retain the current active tab in a variable, in order to prevent another cycle for . You can also do:
document.querySelector('.active').classList.remove('active')
But I like to reduce the number of DOM testimony.
Good luck for your apprentissage, rewriting the jQuery code in Vanilla JS, it seems a good way, and you could get a much better understanding of JavaScript.
source share