Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save waqasraza123/7b61ef89e9ef56d799d80cad071df1fb to your computer and use it in GitHub Desktop.
Save waqasraza123/7b61ef89e9ef56d799d80cad071df1fb to your computer and use it in GitHub Desktop.
keywords.select2({
tags: true,
createTag: function (params) {
var term = $.trim(params.term);
var count = 0
var existsVar = false;
//check if there is any option already
if($('#keywords option').length > 0){
$('#keywords option').each(function(){
if ($(this).text().toUpperCase() == term.toUpperCase()) {
existsVar = true
return false;
}else{
existsVar = false
}
});
if(existsVar){
return null;
}
return {
id: params.term,
text: params.term,
newTag: true
}
}
//since select has 0 options, add new without comparing
else{
return {
id: params.term,
text: params.term,
newTag: true
}
}
},
maximumInputLength: 20, // only allow terms up to 20 characters long
closeOnSelect: true
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment