Search code examples
javascriptjquerybootstrap-duallistbox

How do I alert a popup when an option is clicked in the bootstrap duallistbox?


I am using a Bootstrap Dual Listbox plugin in my website. I want to alert a prompt when a specific option is clicked in the select element and make user fill some data and make that data go to the selected elements and not that particular option. I am not able to get the selected option when an option is clicked. I am using the following code -

<h1>Bootstrap Dual List Box</h1>
    <div class="col-md-6">
        <select multiple="multiple" name="duallist">
            <option value="CCN">Complex Coordination</option>
            <option value="IT">Information Technology</option>
            <option value="CM">Case Management</option>
            <option value="DM">Disease Management</option>
            <option value="NM">Network Management</option>
            <option value="OT">Other</option>
        </select>
    </div>
    <br/>
<script>
$(document).ready(function(){
    var listBox = $("select[name='duallist']").bootstrapDualListbox({
        nonSelectedListLabel:'Non-Selected',
        selectedListLabel:'Selected',
        filterPlaceHolder:'Search',
        moveAllLabel:'Move All'
    });

    $("select[name='duallist_helper1']").on('change',function(){
        alert($("select[name='duallist_helper1']").find('option:selected').text());
        });
    });
});
</script> 

The alert statement return empty value.


Solution

  • Get selected value with $(this).val(), and you need to get selected text with each

    $(document).ready(function() {
      var listBox = $("select[name='duallist']").bootstrapDualListbox({
        nonSelectedListLabel: 'Non-Selected',
        selectedListLabel: 'Selected',
        filterPlaceHolder: 'Search',
        moveAllLabel: 'Move All'
      });
    
      $("select[name='duallist_helper1']").on('change', function(e) {
        let val = $(this).val();
        $("select[name='duallist'] option:selected").each(function() {
          var $this = $(this);
          if ($this.length) {
            var selText = $this.text();
            alert(selText);
          }
        });
    
      });
    });
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap4-duallistbox/4.0.1/jquery.bootstrap-duallistbox.min.js" integrity="sha256-InGbz8oJQNJR4OYGCUMcTAtuGJ+J6jGm3vpSgb6YAoc=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap4-duallistbox/4.0.1/bootstrap-duallistbox.min.css" integrity="sha256-buHEe+156Hk0w29lJZctyXXfZl4mb8OFe1M6QfDanMs=" crossorigin="anonymous" />
    
    <h1>Bootstrap Dual List Box</h1>
    <div class="col-md-6">
      <select multiple="multiple" name="duallist">
        <option value="CCN">Complex Coordination</option>
        <option value="IT">Information Technology</option>
        <option value="CM">Case Management</option>
        <option value="DM">Disease Management</option>
        <option value="NM">Network Management</option>
        <option value="OT">Other</option>
      </select>
    </div>