From: Mark Otto Date: Wed, 24 Jun 2026 22:30:08 +0000 (-0700) Subject: Docs: show how to open/close/toggle a menu via the JS API X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e61ff07b0bf971e4aeb940662c8c5b10ba7d058;p=thirdparty%2Fbootstrap.git Docs: show how to open/close/toggle a menu via the JS API The Via JavaScript section only showed instantiation. Add an example using getOrCreateInstance().show()/hide()/toggle(). Fixes #37042. --- diff --git a/site/src/content/docs/components/menu.mdx b/site/src/content/docs/components/menu.mdx index 622d9d0472..c12ee1cc57 100644 --- a/site/src/content/docs/components/menu.mdx +++ b/site/src/content/docs/components/menu.mdx @@ -604,6 +604,17 @@ const menuElementList = document.querySelectorAll('[data-bs-toggle="menu"]') const menuList = [...menuElementList].map(menuToggleEl => new bootstrap.Menu(menuToggleEl)) ``` +To open, close, or toggle a menu programmatically, get its instance from the trigger element (the one with `data-bs-toggle="menu"`) and call the corresponding method: + +```js +const menuToggleEl = document.querySelector('#myMenuToggle') +const menu = bootstrap.Menu.getOrCreateInstance(menuToggleEl) + +menu.show() +menu.hide() +menu.toggle() +``` + ### Dependencies The menu plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source: