Johntk February 2016

Pass currently selected list item from bootstrap plugin selectpicker into JS

I'm new to JS, I'm trying to access a selected value from a select list using the selectpicker plugin from bootstrap.

Currently my code gives me the entire list rather than just the currently selected item.

<div class="form-group">
                <select class="selectpicker" name="applicationSelect">
                    <option value="1">app 1</option>
                    <option value="2">app 2</option>
                    <option value="3">app 3</option>
                </select>
            </div>

Then in my script.js file I have:

var appName = $('select[name=applicationSelect]').val(1).text();

The retrieved value will then be based onto an Ajax event, but I'm struggling to find a working example doing what I'm trying to do.

How do I retrieve only the currently selected item?

Answers


Lucas Rodriguez February 2016

This should do the trick:

 $('select[name=applicationSelect]').val() 

If you specify val() with a parameter, you are actually setting the value. Without parameters, the method will just return the current value.

You can find more information here.

Post Status

Asked in February 2016
Viewed 3,818 times
Voted 5
Answered 1 times

Search




Leave an answer