Last active
January 3, 2016 23:49
-
-
Save chrisbuttery/8537665 to your computer and use it in GitHub Desktop.
Toggle Dual Data. Added an intermediate .is-loading class.
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
<div class="grid"> | |
<div class="unit"> | |
<span class="js-toggle-el">$100.00</span> | |
<span class="js-toggle-el is-hidden">$200.00</span> | |
</div> | |
<div class="unit"> | |
<span class="js-toggle-el">$1.00</span> | |
<span class="js-toggle-el is-hidden">$2.00</span> | |
</div> | |
<div class="unit"> | |
<!-- grey btn --> | |
<button class="js-toggle-trigger js-toggle-el btn btn--medium btn--inverse" id="ga-1234">Toggle 1</button> | |
<!-- white btn --> | |
<button class="js-toggle-trigger js-toggle-el btn btn--medium btn--secondary is-hidden" id="ga-5678">Toggle 2</button> | |
</div> | |
</div> |
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
module.exports = function(){ | |
var triggers = document.querySelectorAll('.js-toggle-trigger'); | |
var els = document.querySelectorAll('.js-toggle-el'); | |
// toggle class .is-hidden while adding an intermediate 'disabled' attribute | |
function toggle(){ | |
[].forEach.call(els, function(el){ | |
if (el.classList.contains('is-hidden')){ | |
el.disabled = true; | |
el.classList.remove('is-hidden'); | |
setTimeout(function(){ | |
el.disabled = false; | |
}, 500); | |
} else { | |
el.classList.add('is-hidden'); | |
} | |
}); | |
} | |
// Loop through triggersand add an event handler | |
[].forEach.call(triggers, function(trigger){ | |
if (trigger.attachEvent){ | |
trigger.attachEvent('onclick', toggle.bind(this)); | |
} else { | |
trigger.addEventListener('click', toggle.bind(this)); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment