Created
April 27, 2022 03:51
-
-
Save semiarthanoian/f323f79e1627de75afd7ea4e542bfe51 to your computer and use it in GitHub Desktop.
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 toggleTheList = function(event) { | |
var theBtn = event.target; | |
var theList = theBtn.nextElementSibling; | |
var theListIsHidden = theList.className.includes('hidden'); | |
if (theListIsHidden) | |
theList.className = 'dropdown-list shown'; | |
else | |
theList.className = 'dropdown-list hidden'; | |
}; | |
var hideOtherLists = function(event) { | |
var theBtn = event.target; | |
var theList = theBtn.nextElementSibling; | |
var allList = document.getElementsByClassName('dropdown-list'); | |
var allListArray = Array.from(allList); | |
var otherLists = allListArray.filter(function(oneList) { | |
return oneList != theList | |
}); | |
otherLists.forEach(function(oneList) { | |
oneList.className = 'dropdown-list hidden'; | |
}); | |
}; | |
/* Main function */ | |
var toggleList = function(event) { | |
toggleTheList(event); | |
hideOtherLists(event); | |
}; | |
/* Binding to buttons */ | |
var btnList = document.getElementsByClassName('dropdown-btn'); | |
var btnArray = Array.from(btnList); | |
btnArray.forEach(function (btn) { | |
btn.onclick = toggleList; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment