0

I want to be able to display the values I get from a multi select to an empty div using jquery. For example, in the code below, how would I implement this using jquery?

<select multiple data-style="bg-white rounded-pill px-4 py-3 shadow-sm" class="selectnumber w-100 multiple searchable='Search here..'">
  <option>23</option>
  <option>17</option>
  <option>14</option>
  <option>11</option>
  <option>9</option>
</select>

<div class="numberselected">
  numbers selected should display here...
</div>

3
  • It looks like you are using some library wrapper for multiselect. What lib is it? Commented Dec 10, 2019 at 10:16
  • duplicate: stackoverflow.com/questions/3243476/… Commented Dec 10, 2019 at 10:21
  • I am using select2, a jquery plugin Commented Dec 10, 2019 at 10:28

5 Answers 5

2

You can do something below

$(document).on('change', '.selectnumber', function(e){

var selected =   $('.selectnumber').val();
$('.numberselected').text(selected);


//console.log(selected);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select multiple data-style="bg-white rounded-pill px-4 py-3 shadow-sm"
                        class="selectnumber w-100 multiple searchable='Search here..'">
                        <option>23</option>
                        <option>17</option>
                        <option>14</option>
                        <option>11</option>
                        <option>9</option>
                    </select><!-- End -->
                    <div class="numberselected">
                         //numbers selected should display here. 
                    </div>

Sign up to request clarification or add additional context in comments.

Comments

1

You don't have option values, so i will show 2 variants.

$('#my-select').change(function(){
  //Get selected options
  let selectedOptions = $(this).children('option:selected');
  
  //Get text of selected options
  let textValues = selectedOptions
    .map(function(){return $(this).text()})
    .toArray();
    
  //Get values of selected options
  let valValues = selectedOptions
    .map(function(){return $(this).val()})
    .toArray();
    
  //Show them in divs
  $('#show-by-text').html(textValues.join(','));
  $('#show-by-value').html(valValues.join(','));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select multiple id="my-select">
  <option value="1">one</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>
<br>
By text:
<div id="show-by-text">
</div>
By value:
<div id="show-by-value">
</div>

1 Comment

Just FYI if there is no value attribute, then the text is used. The value is always set regardless.
1

Please use jQuery I hope the below code will resolve your issue. Let me know if it didn't work.

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <select multiple data-style="bg-white rounded-pill px-4 py-3 shadow-sm" class="selectnumber w-100 multiple searchable='Search here..'" id="multi-select">
    <option value="23">23</option>
    <option value="17">17</option>
    <option value="14">14</option>
    <option value="11">11</option>
    <option value="9">9</option>
  </select><!-- End -->
  <div class="numberselected"> 
   //numbers selected should display 
  </div>
<script>
$(document).ready(function() {
  $("#multi-select")
    .change(function() {
      var str = "";
      $("#multi-select option:selected").each(function() {
        str += $(this).text() + " ";
      });
      $(".numberselected").text(str);
    })
    .change();
});
</script>

Comments

0

Use jQuery change event.

$(document).ready(function(){
  $("#multi").change(function(){
    $("#numberselected").text($("#multi").val())
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<select multiple data-style="bg-white rounded-pill px-4 py-3 shadow-sm"
    id="multi" class="selectnumber w-100 multiple searchable='Search here..'">
    <option>23</option>
    <option>17</option>
    <option>14</option>
    <option>11</option>
    <option>9</option>
</select>
<div class="numberselected" id="numberselected">
    numbers selected should display here...
</div>

Comments

0

Use JQ:

   $(document).ready(function(){
    $('#test').on('change', function() {
      console.log( $(this).val() );
    });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- begin snippet: js hide: false console: true babel: null -->

        <select id ="test" multiple data-style="bg-white rounded-pill px-4 py-3 shadow-sm" class="selectnumber w-100 multiple searchable='Search here..'">
          <option>23</option>
          <option>17</option>
          <option>14</option>
          <option>11</option>
          <option>9</option>
        </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.