Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get selected value from a bootstrap select drop down

I'm trying to get the selected text, not value, from my bootstrap drop down, but my .text() statement is returning a string that contains all the values with a '\n' in between.

Here is my rendered html

<select class="form-control" id="SpaceAccommodation" name="YogaSpaceAccommodation">
    <option selected="selected" value="0">1-4</option>
    <option value="1">5-9</option>
    <option value="2">10-15</option>
    <option value="3">16-20</option>
    <option value="4">20+</option>
</select>

Here is my javascript, but selectedText returns '5-9\n10-15\n16-20\n20+' I want it to return 5-9 or 10-15, etc..

$('#SpaceAccommodation').change(function () {

    var selectedText = $(this).text();
});
like image 769
user1186050 Avatar asked Apr 24 '15 22:04

user1186050


People also ask

How do I get the value of a DropDownList?

To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).

How do you display a selected value in the Bootstrap multiple dropdown from the database?

1st. take all venue_id`s in array & remove duplicate values, then by mappint this array with $venue display option... 2nd . to display selected values... check new venue id array with venue in <option> tag if it is equal then echo selected..

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.


3 Answers

You can get the selected value's text with $(this).find("option:selected").text().

$('#SpaceAccommodation').change(function () {
    var selectedText = $(this).find("option:selected").text();
    
    $(".test").text(selectedText);
});
<script src="https://code.jquery.com/jquery-1.6.4.min.js"></script>
<select class="form-control" id="SpaceAccommodation" name="YogaSpaceAccommodation">
    <option selected="selected" value="0">1-4</option>
    <option value="1">5-9</option>
    <option value="2">10-15</option>
    <option value="3">16-20</option>
    <option value="4">20+</option>
</select>
<div class="test"></div>
like image 142
Nick Bartlett Avatar answered Oct 24 '22 17:10

Nick Bartlett


Fiddle for you

 $(document).ready(function () {
     $('.chzn-select').change(function () {
         alert( $('.chzn-select option:selected').text());
     });
 });

<select id="second" class="chzn-select" style="width: 100px">
    <option value="1">one</option>
    <option value="2">two</option>
</select>

This is based on the css3 psuedo-class :selected. It's very similar to :checked, I couldn't find docs for :selected

like image 26
J-Dizzle Avatar answered Oct 24 '22 17:10

J-Dizzle


In case anyone cares, I've got another solution. I just looked at the arguments from the docs. You can do something like this (Assuming you've set the value tag of the option element.:

 $('#type_dropdown')
                .on('changed.bs.select',
                    function(e, clickedIndex, newValue, oldValue) {
                        alert(e.target.value);
                    });
        });

See https://silviomoreto.github.io/bootstrap-select/options/

like image 1
coolboyjules Avatar answered Oct 24 '22 17:10

coolboyjules