0

I have drop down list as below.

     <select id="checkOwner" multiple="multiple" onchange="copyValue()">
       <option value="FIRSTNAME">First Name</option>
       <option value="LASTNAME">Last Name</option>
     </select>

I used Below javascript to add checkbox

$(function() {
  $('#checkOwner').multiselect({
   });  
});

I used below javascript to copy selected value to text field.

function copyValue() {
  var dropboxvalue = document.getElementById('checkOwner').value;
  document.getElementById('mytextbox').value = dropboxvalue;
}

But the problem is, this copy only one value. I want to copy all the selected values. How can I do this?

Dinu
  • 159
  • 1
  • 11
  • Does this answer your question? [How to get multiple select box values using jQuery?](https://stackoverflow.com/questions/3243476/how-to-get-multiple-select-box-values-using-jquery) – Joundill Jun 02 '20 at 02:50
  • @Joundill I tried. But it's not work. – Dinu Jun 02 '20 at 03:09

2 Answers2

1

Loop through the option and put selected values in a string and then output the string on the textbox

function copyValue() {

  var str = "";
  for (var option of document.getElementById('checkOwner').options) {
    if (option.selected) {
      str+= option.value+" ";
    }

    document.getElementById('mytextbox').value = str;
  }
}
Hardood
  • 503
  • 1
  • 5
  • 15
  • What happened to the answer? – Hardood Jun 02 '20 at 03:51
  • can you help me for another problem. I have two drop downs. When i select value from one dropdown another selected value disappear from textfield. Do you have any solution for this? – Dinu Jun 02 '20 at 04:15
  • Please post more details about it and i will help you. if you added a new question it will be fine. – Hardood Jun 02 '20 at 04:23
  • I posted a new question. https://stackoverflow.com/questions/62144811/copy-selected-multiple-values-to-the-textbox-from-more-than-one-drop-down – Dinu Jun 02 '20 at 04:25
0

my approach is loop on the options then check the condition if isSelected

html

<select name="checkOwner" multiple="multiple" onchange="copyValue(this)">

js

const copyValue = me => {
    let y = Array.from(document.querySelectorAll('select option')).map(x => x.selected ? x.value : '')
    document.getElementById('mytextbox').value = y.join(' ');
}