Skip to content

Instantly share code, notes, and snippets.

@chrisbuttery
Last active January 3, 2016 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbuttery/8537665 to your computer and use it in GitHub Desktop.
Save chrisbuttery/8537665 to your computer and use it in GitHub Desktop.
Toggle Dual Data. Added an intermediate .is-loading class.
<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>
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