Skip to content

Instantly share code, notes, and snippets.

@vinnihoke
Last active June 14, 2019 01:27
Show Gist options
  • Save vinnihoke/d9c8bb7b293ab74e2ec9472cd800be0a to your computer and use it in GitHub Desktop.
Save vinnihoke/d9c8bb7b293ab74e2ec9472cd800be0a to your computer and use it in GitHub Desktop.
class Tab {
constructor(element) {
// Assign this.element to the passed in DOM element
this.element = element;
// Get the custom data attribute on the Link
this.tabNumber = element.dataset.tab;
// Using the custom data attribute get the associated Item element
this.itemElement = document.querySelector(`div.tabs-item[data-tab="${this.tabNumber}"]`);
// Using the Item element, create a new instance of the TabItem class
this.itemElement = new TabItem(this.element);
// Add a click event listener on this instance, calling the select method on click
this.itemElement.element.addEventListener("click", this.select.bind(this));
};
select() {
console.log("Select is working on the Tab class");
// Get all of the elements with the tabs-link class
const links = document.querySelector(".tabs-link");
// Using a loop or the forEach method remove the 'tabs-link-selected' class from all of the links
Array.from(links).forEach(link => {
this.element.classList.remove(".tabs-link-selected");
});
// Add a class named "tabs-link-selected" to this link
this.element.classList.add(".tabs-link-selected");
// Call the select method on the item associated with this link
this.element.addEventListener("click", this.select.bind(this));
}
}
class TabItem {
constructor(element) {
// Assign this.element to the passed in element
this.element = element;
}
select() {
console.log("Select is working on the TabItem class");
// Select all ".tabs-item" elements from the DOM
this.element = document.querySelectorAll(".tabs-item");
console.log("Select is working!!");
// Remove the class "tabs-item-selected" from each element
this.element.classList.remove(".tabs-item-selected");
// Add a class named "tabs-item-selected" to this element
this.element.classList.add(".tabs-item-selected");
}
}
/* START HERE:
- Select all classes named ".tabs-link" and assign that value to the links variable
- With your selection in place, now chain a .forEach() method onto the links variable to iterate over the DOM NodeList
- In your .forEach() method's callback function, return a new instance of TabLink and pass in each link as a parameter
*/
const tabs = document.querySelectorAll(".tabs-link");
tabs.forEach(tab => new Tab(tab));
//We're passing the tab into the new tab class
@vinnihoke
Copy link
Author

Ah!! Super helpful! Thanks Jonathan!

@vinnihoke
Copy link
Author

So I'm getting the select button to fire on the Tab class, but nothing is happening. No errors, but also nothing that is supposed to happen. I am unsure where I'm going wrong with this one.

@vinnihoke
Copy link
Author

I feel like it has something to do with const links on line 22. I feel like I need to use a this.element something to make it work.

@macjabeth
Copy link

Take a look at the select methods and how you're using the query selectors to select data. How are they being returned? As a node element or as an array?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment