Default values in a Select2

Asked

Viewed 451 times

-3

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

Example:

I have a list of the database'name','Nome2','name3'. I want when loading the tab to be selected by default'name' and'Nome2'

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

inserir a descrição da imagem aqui

  • This question needs more detail. What code have you experienced? what error are you making? what is your current html structure?

  • I voted to reopen, although the question is still poorly worded, because I understood what the AP wants and still need to quote a question on version 4.0 of Select2 where changes a detail.

1 answer

1

As it is in the documentation of the Select2, you can create the options dynamically 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>
  • This reply should be a comment, to facilitate users

  • updated the information

Browser other questions tagged

You are not signed in. Login or sign up in order to post.