Currently, I'm diving into learning Bootstrap and one thing I'd like to implement is having an x
next to each tab name for easy closing:
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2924243f383f392a3b0b7e65796578">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" id="tab-li-1">
<button
class="nav-link active"
data-bs-toggle="tab"
data-bs-target="#tab-1"
type="button"
>
First
<button
class="btn-close"
onclick="
document.getElementById(`tab-li-1`).remove();
document.getElementById(`tab-1`).remove();
"
></button>
</button>
<li class="nav-item" id="tab-li-2">
<button
class="nav-link"
data-bs-toggle="tab"
data-bs-target="#tab-2"
type="button"
>
Second
<button
class="btn-close"
onclick="
document.getElementById(`tab-li-2`).remove();
document.getElementById(`tab-2`).remove();
"
></button>
</button>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="tab-1" tabindex="0">Some text here for `First`</div>
<div class="tab-pane fade" id="tab-2" tabindex="0">More text here for `Second`</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adcfc2c2d9ded9dfccdded98839f839e">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
After some trial and error, I managed to come up with a workaround by nesting another button inside my initial button and then executing:
document.getElementById(tabId).remove();
document.getElementById(tabContentId).remove();
However, I recently discovered that nesting a button within another button is considered invalid. So, I'm reaching out to ask if anyone has suggestions for a better alternative approach?