I am using the Select2 tag input jQuery plugin.
I call select items and preselected items via two json formatted variables and using initSelection option to set preselected Items.
All things work perfectly, but when I choose a new item from the select box, previous items are removed from it.
How can I solve this problem?
var allTags = [{
"tag_id": 1,
"tag_title": "Laravel"
}, {
"tag_id": 3,
"tag_title": "jQuery"
}, {
"tag_id": 4,
"tag_title": "HTML"
}];
var postTags = ["tag_id": 3, "tag_title": "jQuery"
}, {
"tag_id": 4,
"tag_title": "HTML"
}
}];
var allTags = $.map(allTags, function (obj) {
obj.id = obj.id || obj.tag_id;
obj.text = obj.text || obj.tag_title;
return obj;
});
var postTag = $.map(postTags, function (obj) {
obj.id = obj.id || obj.tag_id;
obj.text = obj.text || obj.tag_title;
return obj;
});
$('[id^="meta_keywords"]').select2({
dir: "rtl",
tags: true,
initSelection: function (element, callback) {
callback(postTag);
},
data: allTags
});
This is a JsFiddle Example: