Reputation: 7554
Current code shows undefined
.
And the new term also not added to the list, the alert seem like not working.
$.getScript('//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.full.js',function(){
$("#mySel").select2({ width:'240px', allowClear:true, "language": { "noResults": function(term) { /* customize the no matches output */ return "Create" } },
escapeMarkup: function (markup) {
return markup;
}
}) .parent().find('.select2-with-searchbox').on('click','#addNew',function(){ /* add the new term */ var newTerm = $('#newTerm').val(); alert('adding:'+newTerm); $(''+newTerm+'').appendTo('#mySel'); $('#mySel').select2('val',newTerm); // select the new term $("#mySel").select2('close'); // close the dropdown })
});
http://www.codeply.com/go/LxasHWd0rd
Upvotes: 1
Views: 592
Reputation: 2267
Per the select2 documentation, you can use the "tags" option to achieve what you're trying to do. Select2 already does that for you:
"If the tags option is passed into Select2, if a user types anything into the search box which doesn't already exist, it will be displayed at the top and the user will be able to select it."
Upvotes: 1