There are many textboxes (Django CharField) for which I am using tokenInput as the JS plugin for autocomplete feature. To intialize each of them, I need to set prepopulate with different tag values. I want to avoid following code duplication as shown below.
$("#id_tags_1").tokenInput("/xyz/tag_search/", {
theme: "facebook",
onAdd: function(item){
tag_ids.push(item.id);
$("#id_tag_domain").val(tag_ids.join(','));
},
onDelete: function(item){
tag_ids = _.without(tag_ids, item.id);
$("#id_tag_domain").val(tag_ids.join(','));
},
preventDuplicates: true,
tokenLimit: 3,
prePopulate: tags_val[0],
});
$("#id_tags_2").tokenInput("/xyz/tag_search/", {
theme: "facebook",
onAdd: function(item){
tag_ids.push(item.id);
$("#id_tag_domain").val(tag_ids.join(','));
},
onDelete: function(item){
tag_ids = _.without(tag_ids, item.id);
$("#id_tag_domain").val(tag_ids.join(','));
},
preventDuplicates: true,
tokenLimit: 3,
prePopulate: tags_val[1],
});
I came across another SO post where regular expression like this [^id_tags_]
was suggested but I don't know how to select each tag value corresponding to different tags. I think part of the problem is that I don't know how to access the tag id else I could have split it and extracted the number. If there is any other elegant solution, please do suggest.