Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tagging text in jquery select2

<select multiple id="e1" style="width:300px">
        <option value="AL">Alabama</option>
        <option value="Am">Amalapuram</option>
        <option value="An">Anakapalli</option>
        <option value="Ak">Akkayapalem</option>
        <option value="WY">Wyoming</option>
    </select>

$("#e1").select2({'placeholder': ' ','allowClear': true});

it is working fine for matched text. But i want to tag text(which doesnot match any option). Kindly help.

please find the demo code in following link. http://jsfiddle.net/marcusasplund/jEADR/2/

like image 311
user3816152 Avatar asked Nov 19 '25 13:11

user3816152


2 Answers

You have the tags option, but it can't be used with select element

$("#e1").select2({
  tags: [{
    "id": "AL",
    "text": "Alabama"
  }, {
    "id": "Am",
    "text": "Amalapuram"
  }, {
    "id": "An",
    "text": "Anakapalli"
  }, {
    "id": "Ak",
    "text": "Akkayapalem"
  }, {
    "id": "WY",
    "text": "Wyoming"
  }]
});
<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.css"/>

<input id="e1" style="width:300px" />
like image 67
Arun P Johny Avatar answered Nov 22 '25 03:11

Arun P Johny


There is an option called tags in select2 which allows you to unmatched text to tags but there are some problems here.

  • It will not allow you to set tags option if you are binding select2 plugin on select. So for this you need to bind select2 on input.

SOURCE QUESTION

  • Select2 Version 4.0 allows you to add tags option to select control too.

So the solution is either you need to upgrade to the latest plugin or you need to bind select2 on input control.

like image 40
Guruprasad J Rao Avatar answered Nov 22 '25 03:11

Guruprasad J Rao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!