0

I have a html

<select name="availableon[]" class="form-control" multiple>
     <option value="pc">PC</option>
     <option value="iphone">iPhone</option>
     <option value="android">Android</option>
 </select>

And in my AJAX success function I have the value

availableon = 'pc,iphone'

Now I want to explode the availableon variable and select the multiple select based on value using javascript or jquery. How will I do that?

Ali Zia
  • 3,825
  • 5
  • 29
  • 77
  • If you are looking for a pure js solution, check: https://stackoverflow.com/questions/16582901/javascript-jquery-set-values-selection-in-a-multiple-select/43255752#43255752 – kind user Apr 06 '17 at 12:58

1 Answers1

1

You can change the data to an array and then set the value with the array

var availableon = 'pc,iphone'

var dataarray = availableon.split(",");

$("select").val(dataarray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="availableon[]" class="form-control" multiple>
     <option value="pc">PC</option>
     <option value="iphone">iPhone</option>
     <option value="android">Android</option>
 </select>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77