I am trying to set default value of select2 single select dropdown : http://ivaynberg.github.io/select2/.
I have the select2 dropdown working. But i am not sure how to set the default value. For example, say the list of values is - test1, test2, test3. How do i set 'test1' as the default value of the single select drop down.
$(function(){
var testData = [{id:0, text:"test1"}, {id:1, text:"test2"}, {id:2, text:"test3"}];
$('#select2customdata').select2({placeholder:"Search Stuff", data: {results: testData, text:'text'}});
$('#select2customdata').select2().select2("val", "test1");
});
And html has :
<input type="hidden" id="select2customdata" style="width:300px"> </input>
As you can see, i am using the select2("val", "some-val") to set a default value. It does not work for me.
EDIT: I figured out the solution, if val is used, have to have initselection in the options. Other solution is to use :
$('#select2customdata').select2("data", {"id": id_val, "text": text_val})