- const getChildPosition = function (element) {
- var parent = element.parentNode;
- var i = 0;
- for (var i = 0; i < parent.children.length; i++) {
- if (parent.children[i] === element) {
- return i;
- }
- }
+ // The tab switching function
+ const switchTab = (oldTab, newTab) => {
+ newTab.focus();
+ // Make the active tab focusable by the user (Tab key)
+ newTab.removeAttribute('tabindex');
+ // Set the selected state
+ newTab.setAttribute('aria-selected', 'true');
+ newTab.classList.add('active');
+ oldTab.removeAttribute('aria-selected');
+ oldTab.setAttribute('tabindex', '-1');
+ oldTab.classList.remove('active');
+ // Get the indices of the new and old tabs to find the correct
+ // tab panels to show and hide
+ let index = Array.prototype.indexOf.call(tabs, newTab);
+ let oldIndex = Array.prototype.indexOf.call(tabs, oldTab);
+ panels[oldIndex].hidden = true;
+ panels[index].hidden = false;
+ }