Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Default Value in tagging in select2 jquery

I am using select2 (http://ivaynberg.github.io/select2/) for my tagging input. from the example in using select2 tagging the code is look like this.

 $("#e12").select2({tags:["red", "green", "blue"]});

now my problem is how can I insert default value in my input just like in the page example in http://ivaynberg.github.io/select2/ at Tagging Support, the input has a default value of 'brown', 'red', & 'green'.

like image 306
gadss Avatar asked Jun 07 '13 23:06

gadss


1 Answers

You can initialize the Select2 control by providing a value for the hidden input:

<input type="hidden" id="e12" value="brown,red,green" />

And you can set the value of the Select2 control by passing an array to the 'val' method:

$('#tagSelect').select2('val', ['brown','red','green']);

jsfiddle demo

like image 60
John S Avatar answered Nov 09 '22 12:11

John S