I need to get the value a user selects from a twitter bootstrap dropdown. Once the user selects a value I want the dropdown to display that value. So, the user selects 'pending' then I want to change the selected dropdown value from that and, when the user completes the form, I want to retrieve the selected value. Would someone be able to point me in the right direction?
<div class="btn-group">
<i class="dropdown-arrow dropdown-arrow-inverse"></i>
<button class="btn btn-primary">status</button>
<button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-inverse">
<li><a href="#fakelink">pending</a></li>
<li><a href="#fakelink">active</a></li>
<li><a href="#fakelink">discontinued</a></li>
</ul>
</div>
Method 1: Using the value property: The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
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.
I had the same issue. The solution is pretty easy and I think the answers above may be overcomplicating a bit.
Just use $(this).text()
Assign a class name to the "" bit then..
$(".yourUlClassName li a").click( function() {
var yourText = $(this).text();
});
Bootstrap provides you with minimum required functionality for your implementation, they aren't dropdowns instead they are mere ul, li's with css applied on it and some events registered in the bootstrap plugin for toggling the button to display them as dropdown. This one you need to do on your own.
Set up some identifier to the status button say a className btnStatus
and bind a click event to the dropdown links. and do something like this?
$('.dropdown-inverse li > a').click(function(e){
$('.btnStatus').text(this.innerHTML);
});
Demo
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