Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap: How do I get the selected value of dropdown?

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>
like image 655
Matthew David Jankowski Avatar asked Nov 01 '13 21:11

Matthew David Jankowski


People also ask

How do you display a selected value in a drop down list?

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.

How do I select a dropdown in Bootstrap?

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.


2 Answers

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();
});
like image 195
BandfuzzAlex Avatar answered Sep 21 '22 03:09

BandfuzzAlex


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

like image 40
PSL Avatar answered Sep 19 '22 03:09

PSL