]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Docs: show how to open/close/toggle a menu via the JS API
authorMark Otto <markdotto@gmail.com>
Wed, 24 Jun 2026 22:30:08 +0000 (15:30 -0700)
committerMark Otto <markdotto@gmail.com>
Wed, 24 Jun 2026 22:30:08 +0000 (15:30 -0700)
The Via JavaScript section only showed instantiation. Add an example
using getOrCreateInstance().show()/hide()/toggle(). Fixes #37042.

site/src/content/docs/components/menu.mdx

index 622d9d0472da30443376872c107eec024323dcaa..c12ee1cc571e21ec653b33956151a21301b89e17 100644 (file)
@@ -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: