Created
September 17, 2016 11:45
-
-
Save bjankord/42750f731b9413b52727afc6b7004e98 to your computer and use it in GitHub Desktop.
JS to toggle data attributes from: https://toddmotto.com/stop-toggling-classes-with-js-use-behaviour-driven-dom-manipulation-with-data-states/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nav = document.querySelector('.nav__toggle'); | |
var toggleState = function (elem, one, two) { | |
var elem = document.querySelector(elem); | |
elem.setAttribute('data-state', elem.getAttribute('data-state') === one ? two : one); | |
}; | |
nav.onclick = function (e) { | |
toggleState('.nav ul', 'closed', 'open'); | |
e.preventDefault(); | |
}; | |
// ES5 using .bind() #ftw | |
nav.addEventListener('click', toggleState.bind(null, '.nav ul', 'closed', 'open'), false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment