Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap-select on click get clicked value

I am using a jQuery plug-in: bootstrap-select.js my HTML is select(multiple) -> option. I would like to get the currently clicked on option value when a user selects or deselects the option.

Example: User selects Item 4 = console.log item 4. Then the user also selects Item 2 = console.log item 2. Currently i'm getting item 4, item 2... they are always in a array and not individual.

My end goal is to show and hide divs on the page depending on what the user has selected. There will be multiple select option fields.

HTML code:

<fieldset class="dreamT">
   <select name="team" id="team" multiple class="dropdown selectpicker show-menu-arrow show-tick form-control" title="Pelaajat" data-width="100%" data-size="auto" multiple data-selected-text-format="count > 2">
    <optgroup>
        <option value="item 1">Item 1</option>
        <option value="item 2">Item 2</option>
        <option value="item 3">Item 3</option>
        <option value="item 4">Item 4</option>
        <option value="item 5">Item 5</option>
        <option disabled value="item 6">Item 6</option>
    </optgroup>
   </select>
</fieldset>

JS code:

$("select#team").on("change", function(value){
   var This      = $(this);
   var selectedD = $(this).val();
   console.log(selectedD);
});

Current output: ["item 1", "item 3", "item 4", "item 5"]

Plug-in site: bootstrap-select

like image 402
Bonttimo Avatar asked Apr 29 '16 17:04

Bonttimo


People also ask

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

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

What is selectpicker('refresh')?

. selectpicker('refresh') To programmatically update a select with JavaScript, first manipulate the select, then use the refresh method to update the UI to match the new state. This is necessary when removing or adding options, or when disabling/enabling a select via JavaScript.

How to use selectpicker in bootstrap?

selectpicker({ style: 'btn-info', size: 4 }); }); Your script is declared and therefore executed before the <select class="selectpicker"> -declaration, causing bootstrap-select never being initialized opon . selectpicker (selectpicker doesnt exists when the script is runned).

How to destroy selectpicker?

.selectpicker('destroy') To programmatically destroy the bootstrap-select, use the destroy method. $('. selectpicker'). selectpicker('destroy');


2 Answers

As described in the bootstrap-select events you can use changed.bs.select event.

This event fires after the select's value has been changed. It passes through the following 4 arguments:

  • event
  • clickedIndex
  • isSelected
  • previousValue

$(function () {
  $('#team').selectpicker();
  $("#team").on("changed.bs.select", function(e, clickedIndex, isSelected, oldValue) {
      if (clickedIndex == null && isSelected == null) {
          var selectedItems = ($(this).selectpicker('val') || []).length;
          var allItems = $(this).find('option:not([disabled])').length;
          if (selectedItems == allItems) {
              console.log('seleted all');
          } else {
              console.log('deseleted all');
          }
      } else {
          var selectedD = $(this).find('option').eq(clickedIndex).text();
          console.log('selectedD: ' + selectedD +  ' oldValue: ' + oldValue);
      }
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>



<fieldset class="dreamT">
    <select name="team" id="team" multiple class="selectpicker show-menu-arrow show-tick form-control" title="" multiple data-actions-box="true"
            data-width="100%" data-size="auto" multiple data-selected-text-format="count > 2">
        <optgroup>
            <option value="item 1">Item 1</option>
            <option value="item 2">Item 2</option>
            <option value="item 3">Item 3</option>
            <option value="item 4">Item 4</option>
            <option value="item 5">Item 5</option>
            <option disabled value="item 6">Item 6</option>
        </optgroup>
    </select>
</fieldset>
like image 93
gaetanoM Avatar answered Oct 20 '22 23:10

gaetanoM


Or just have the event on the option elements...

$("option").on("click", function(value){
        var This = $(this);
        var selectedD = $(this).val();
        console.log(selectedD);
});

https://jsfiddle.net/adjavaherian/ssqz4sh8/

like image 2
4m1r Avatar answered Oct 21 '22 00:10

4m1r