Skip to content

Instantly share code, notes, and snippets.

@pete-murphy
Last active February 2, 2019 16:54
Show Gist options
  • Save pete-murphy/bc94e07b4c7ef3ece71dde5df7006255 to your computer and use it in GitHub Desktop.
Save pete-murphy/bc94e07b4c7ef3ece71dde5df7006255 to your computer and use it in GitHub Desktop.
Sprint Challenge stretch refactored to use functions
const tabs = document.querySelectorAll(".tab")
const cards = document.querySelectorAll(".card")
const show = tab => {
cards.forEach(({ dataset, style }) => {
tab === "all"
? (style.display = "flex")
: dataset.tab === tab && (style.display = "flex")
})
tabs.forEach(({ dataset, classList }) => {
dataset.tab === tab && classList.add("active-tab")
})
}
const hide = tab => {
cards.forEach(({ dataset, style }) => {
tab === "all"
? (style.display = "none")
: dataset.tab === tab && (style.display = "none")
})
tabs.forEach(({ dataset, classList }) => {
dataset.tab === tab && classList.remove("active-tab")
})
}
;(function run(current = "all", last) {
last && hide(last)
show(current)
tabs.forEach(tab => {
tab.onclick = () => run(tab.dataset.tab, current)
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment