1

I have a multiple select dropdown using selectpicker and i want to display the selected text in another input box with @selected name not the value. enter image description here In this on click of plus a dropdown should open and after selecting the plus remains but the selected text has to display in another input box. here is the code that i have tried HTML code

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

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script> 


 <select id='recruiter' name="recruiter"  class="selectpicker" multiple>
     <optgroup label="Camping">
    <option value="1">Tent</option>
    <option value="2">Flashlight</option>
    <option value="3">Toilet Paper</option>
  </optgroup>
    <optgroup label="fruits">
    <option value="6">apple</option>
    <option value="7">orange</option>
    <option value="8">banana</option>
  </optgroup>
  </select>
 <input type="text" class="width-100 chat-borderTB pad-5" placeholder="comment" aria-describedby="sizing-addon2" name="msg" id="msg">


  <script type="text/javascript">
         $("#recruiter").on("change",function(){

           update();
         });

         function update() {

var selected=[];
 $('#recruiter :selected').each(function(){
     selected[$(this).val()]=$(this).text();
    });
 // alert($.trim(selected));
console.log(selected);
         $("#msg").val(selected);
         }
      </script>

Can u suggest how to do this.

Sam
  • 1,381
  • 4
  • 30
  • 70

1 Answers1

0

Try this code:

var selected=[];
 $('#recruiter option:selected').each(function(){
     selected[$(this).val()]=$.trim($(this).text());
    });
console.log(selected);
         $("#msg").val(selected);
         }
Nihar Sarkar
  • 1,187
  • 1
  • 13
  • 26
  • thank you but i need the @ symbol before the selected item eg: if we select a,b,c then it should display as @a @b @c – Sam May 21 '18 at 09:40
  • Simple string concatenation will solve that . selected[$(this).val()]="@"+$.trim($(this).text()); – Nihar Sarkar May 21 '18 at 09:42
  • can we do like on click of a button dropdown opens – Sam May 21 '18 at 09:56
  • check this post : https://stackoverflow.com/questions/430237/is-it-possible-to-use-js-to-open-an-html-select-to-show-its-option-list – Nihar Sarkar May 21 '18 at 09:58
  • can we give the placeholder for the select dropdown using selectpicker – Sam May 21 '18 at 10:03
  • Use title attribute in select element . – Nihar Sarkar May 21 '18 at 10:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171454/discussion-between-nihar-sarkar-and-lakshmi-priya). – Nihar Sarkar May 21 '18 at 10:07
  • in bootstrap selectpicker if we select two or more it will count and it will show as 3 items selected can we remove the word items selected – Sam May 21 '18 at 10:17
  • didn't get you . if you have farther question , please add another question with codes . that will better to understand the issue . Don't forget to mark solved , if the problem of this question is solved . Thank you . – Nihar Sarkar May 21 '18 at 10:20
  • i need to count the number of items selected ,so i am using this – Sam May 21 '18 at 10:22
  • use JS replace() method . https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – Nihar Sarkar May 21 '18 at 11:10