Search code examples
javascriptjqueryhtmlselectdropdown

How to get the selected values of multiple select box in a text field in. I am using select js library


I want to get values of multiple select box in a text field . This is my code. As you can see I am using other third party JS library to make the select box searchable and dropdown although its a multiple select box. For this reason the javascript I am using to get the values of selected option are not coming into the text field.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>

<form method="post" action="">

<div class="form-group">
    <label for="autoform-company-name"><b>Which Industries do you wish to receive alerts about?</b></label>
    <select multiple class="selectpicker" id="autoform-sector" name="autoform-sector" style="height: 10px; overflow-y: scroll;"  data-live-search="true">
                    <option value="Code 01110 ">Code 01110 </option>
                    <option value="Code 01120 ">Code 01120 </option>
                    <option value="Code 01130 ">Code 01130 </option>
                    <option value="Code 97000 ">Code 97000 </option>
                    <option value="Code 98100 ">Code 98100 </option>
        </select>
</div>

<div class="form-group">
<input type="text" class="form-control" id="sector" name="sector" >
</div>           
</form>     


<script type="text/javascript">
    var sel = document.getElementsById ('autoform-sector')[0];
    sel.onclick = function () {
    document.getElementsById ('sector')[0].value += this.value + ' ';
}
</script>

I have seen this question Adding to a HTML text field by clicking items in a multiple select box and tried with this solution. http://jsfiddle.net/wDJLW/1/

But this solution is not working for me. Because I am using another java-script library to make the select box searchable

How to make it work

This is my code


Solution

  • You can also achieve it by jquery too

    $("#autoform-sector").on('change',function(){
        $("#sector").val($(this).val());
    });
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
    
    <form method="post" action="">
    
      <div class="form-group">
        <label for="autoform-company-name"><b>Which Industries do you wish to receive alerts about?</b></label>
        <select multiple class="selectpicker" id="autoform-sector" name="autoform-sector" style="height: 10px; overflow-y: scroll;" data-live-search="true">
          <option value="Code 01110 ">Code 01110 </option>
          <option value="Code 01120 ">Code 01120 </option>
          <option value="Code 01130 ">Code 01130 </option>
          <option value="Code 97000 ">Code 97000 </option>
          <option value="Code 98100 ">Code 98100 </option>
        </select>
      </div>
    
      <div class="form-group">
        <input type="text" class="form-control" id="sector" name="sector">
      </div>
    </form>