Answers for "Open one dropdown at a time"

0

Open one dropdown at a time

var nav = document.querySelector('.my-nav');
nav.addEventListener('toggle', function (event) {
	// Do stuff...
}, true);
Posted by: Guest on August-06-2021
0

Open one dropdown at a time

var nav = document.querySelector('.my-nav');
nav.addEventListener('toggle', function (event) {

	// Only run if the dropdown is open
	if (!event.target.open) return;

	// Get all other open dropdowns and close them
	var dropdowns = nav.querySelectorAll('.dropdown[open]');
	Array.prototype.forEach.call(dropdowns, function (dropdown) {
		if (dropdown === event.target) return;
		dropdown.removeAttribute('open');
	});

}, true);
Posted by: Guest on August-06-2021

Code answers related to "Open one dropdown at a time"

Browse Popular Code Answers by Language