I am using a bootstrap dropdown, and need to have the first option as default. The following doesn't work.
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
CHOOSE FEATURE
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li selected="selected"><a>Feature 1</a></li>
<li><a>Feature 2</a></li>
<li><a>Feature 3</a></li>
<li><a>Feature 4</a></li>
<li><a>Feature 5</a></li>
<li><a>Feature 6</a></li>
</ul>
To open the dropdown menu, use a button or a link with a class of . dropdown-toggle and the data-toggle="dropdown" attribute. The . caret class creates a caret arrow icon (), which indicates that the button is a dropdown.
Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.
Dropdowns are toggleable, contextual overlays for displaying lists of links and more. They're made interactive with the included Bootstrap dropdown JavaScript plugin. They're toggled by clicking, not by hovering; this is an intentional design decision.
Unfortunately, I don't believe you'll be able to achieve this effect using a conventional bootstrap drop down menu.
Unlike a traditional HTML "select", a bootstrap drop down is typically used to group a series of links under a header. When you click a menu item, it doesn't become selected as such, rather an action is usually performed.
I'd advise just using a straightforward HTML select, but borrowing styles from the bootstrap CSS library so it looks consistent. Something along the lines of:
<select class="bootstrap-select">
<option value="1" selected="selected">Feature 1</option>
<option value="2">Feature 2</option>
<option value="3">Feature 3</option>
<option value="4">Feature 4</option>
</select>
Try this:
$(document).ready(function() {
$(".dropdown .dropdown-menu li a")[0].click();
});
This will always select your first li
The attribute "selected" only works in <select>
elements. Unfortunately it does not work in lists.
I believe what you want is:
<select class="form-control" name="features">
<option value="" selected>Feature 1</option>
<option value="">Feature 2</option>
<option value="">Feature 3</option>
<option value="">Feature 4</option>
<option value="">Feature 5</option>
<option value="">Feature 6</option>
</select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With