Created
May 23, 2021 19:32
-
-
Save frankyonnetti/d7f2278d039ad531b6c663441cf3891c to your computer and use it in GitHub Desktop.
jQuery - hide on outside click #jquery #esc
This file contains hidden or 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 subMenuButton = 'ul#primary-menu li.menu-item-has-children > a' | |
var subMenuDropdown = 'ul.submenu.menu-depth-1' | |
// Hide menu drop-down on outside click | |
$(document).on('click', function (e) { | |
if ($(subMenuButton).hasClass('expanded') && $(e.target).closest(subMenuButton).length === 0) { | |
$(subMenuButton).removeClass('expanded') | |
$(subMenuDropdown).hide() | |
} | |
}) | |
// Hide menu drop-down by pressing the ESC key | |
$(document).keyup(function (e) { | |
if ($(subMenuButton).hasClass('expanded') && e.keyCode === 27) { | |
$(subMenuButton).removeClass('expanded') | |
$(subMenuDropdown).hide() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment