I have the code shown below [I am using JQuery and Bootstrap 4]:
$('.dropdown-menu li').on('click', function() {
var getValue = $(this).text();
$('.dropdown-select').text(getValue);
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<div class="col-auto p-2 col-5">
<div class="dropdown">
<button id="dLabel" class="dropdown-select" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dLabel">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
<li>Option 5</li>
<li>Option 6</li>
</ul>
</div>
</div>
by clicking any dropdown option, not selected and don’t show on it.
2
Answers
If I understood correctly. You need something like this:
Defer means — a script executed after the page has finished parsing
Push away from the
<li>
you choose, because there can be more than one.dropdown
on the page.And just add the
active
class to<li>
in this list.