I am using select2 plugin ver 3.5. It is working fine on php file. I am able to select value and able to save them in database.
Scenario
User save the data and after some time he/she wants to update/modify the data. then previously selected value should appear in the form.
I am using Ajax to retrieve the data from database and it is working fine. Issue is i am not able to show selected data in select2 plugin.
I searched on SO and found few relevant questions but i am not able to understand how to use them in my scenario
$("#autofill").change(function() {
var data1= $('option:selected', this).text();
$.ajax({
//type: 'post',
type: "GET",
url:"autofill.php",
cache: false,
// dataType: 'json',
data: 'action1=' + data1,
success: function(data) {
data = JSON.parse( data );
var keywordArray=data.keywords;
$('#tags').select2().select2('val', keywordArray)
But for some reason $('#tags').select2().select2('val', keywordArray)
this is not working.
I found this error. Uncaught query function not defined for Select2 tags
Please advise.
Edit: I checked this question and its bit similar to this but not able to sort out issue.
How to set selected value of jquery select2?
I tried
$('#tags').select2('val', {id: 100, text: 'Lorem Ipsum'});
and
$('#tags').select2('val', 'keywordArray')
but no success.
Edit2:
For some reason something is working on.
$('#tags').select2('val', {id: 1, text: 'test'})
This is working but i am getting [object Object]