class - Toggle modal with vanilla javascript -
i'm used working jquery i'm trying vanilla javascript. have link when clicked reveal account modal.
i want change class of modal when clicked 'modal-visible'. works expected, when click link again close modal, need class change 'modal-hidden'.
i wondered if me that. perhaps needs toggle instead?
var accountmodal = document.getelementbyid("account-modal"); document.queryselector('#account-photo').addeventlistener('click', function(e) { e.preventdefault(); accountmodal.classlist.add('modal-visible'); accountmodal.classlist.remove('modal-hidden'); accountmodal.setattribute('aria-hidden', 'false'); }); <a id="account-photo" href="/customer" tabindex="0" aria-expanded="false">account</a> <div id="account-modal" class="modal-visible" aria-label="account information" aria-hidden="false">account info</div>
here's way of doing if assume begins closed:
document.queryselector('#account-photo').addeventlistener('click', function() {var is_visible = false; return function(e) { e.preventdefault(); if(!is_visible) { accountmodal.classlist.add('modal-visible'); accountmodal.classlist.remove('modal-hidden'); accountmodal.setattribute('aria-hidden', !is_visible); is_visible = true; } else { accountmodal.classlist.remove('modal-visible'); accountmodal.classlist.add('modal-hidden'); accountmodal.setattribute('aria-hidden', !is_visible); is_visible = false; } }});
this method acts manually coded toggle.
Comments
Post a Comment