When attempting to run a code snippet by using the "Run code snippet" button and then clicking the "Open menu" button, nothing happens. However, it should open the menu as expected.
The menu is defined elsewhere in the DOM, and its display property is set to none.
Is there a way to execute this code snippet from this current page?
function xonclick() {
const dropdownButton = document.getElementById('settingsDropDown');
const dropdownMenu = document.getElementById('seadedMenyy');
const clickedButton = document.getElementById('grid_muu');
// Get current dropdown instance
const existingDropdown = bootstrap.Dropdown.getInstance(dropdownButton);
// Toggle behavior
if (existingDropdown) {
existingDropdown.dispose();
dropdownMenu.classList.remove('show');
return;
}
// Get precise button position
const rect = clickedButton.getBoundingClientRect();
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
// Set position with scroll offset
dropdownMenu.style.position = 'absolute';
dropdownMenu.style.left = `${rect.left}px`;
dropdownMenu.style.top = `${rect.bottom + scrollTop}px`;
// Create and show new dropdown instance
const dropdown = new bootstrap.Dropdown(dropdownButton);
dropdown.show();
}
.dropdown-menu {
margin: 0 !important;
padding: 0.5rem 0;
transform: none !important;
}
#settingsDropDown {
position: fixed;
visibility: hidden;
pointer-events: none;
}
<head>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0c01011a1d1a1c0f1e2e5b405d405d">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e5e8e8f3f4f3f5e6f7c7b2a9b4a9b4">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="dropdown">
<button id="settingsDropDown" class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu" id="seadedMenyy">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
<table>
<tr>
<td>Some text</td>
<td><button id="grid_muu" onclick="xonclick()">Open menu</button></td>
</tr>
</table>
</body>