Check out the code snippet below:
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d1f1212090e090f1c0d3d48534e534e">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7b5b8b8a3a4a3a5b6a7fabeb4b8b9a497e6f9e6e6f9e4">[email protected]</a>/font/bootstrap-icons.min.css">
<script src="https://code.jquery.com/jquery-3.7.1.slim.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63010c0c17101711021323564d504d50">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button <i class="bi bi-x inside-button"></i>
</button>
<ul class="dropdown-menu">
<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>
<script>
$('.inside-button').on('click', function(e) {
console.log(new Date());
});
</script>
</body>
</html>
The code shows a bootstrap icon within a button dropdown that triggers a click event (through the class inside-button
) to clear the dropdown content.
However, the issue arises where clicking the icon also opens and closes the dropdown. The desired functionality is for the click event to only clear the content without affecting the dropdown's open/close status. How can this be achieved?
Please note that I have extensively searched on Stack Overflow before reaching out, specifically looking for solutions related to Bootstrap 5.