Default values in a select2 [closed]

-3

Well, I need to know how to call a select2 with multiple default values. That is, how to leave pre-existing options in select2.

Example:

I have a list of the database 'name', 'name2', 'name3'. I want when you load the page, select by default 'name' and 'name2'

Detail, I'm not using a list but a <input type="text">

    
asked by anonymous 26.01.2017 / 22:50

1 answer

1

As in the Select2 documentation itself, you can dynamically create the options through an array:

<script type="text/javascript">
  var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];

  $(".js-example-data-array").select2({
    data: data
  });

  $(".js-example-data-array-selected").select2({
    data: data
  });
</script>
<select class="js-example-data-array"></select>

<select class="js-example-data-array-selected">
  <option value="2" selected="selected">duplicate</option>
</select>
    
26.01.2017 / 23:07