I need to add "Add new item" option in Select2

Prabhakarank picture Prabhakarank · Jul 28, 2016 · Viewed 29.4k times · Source

I want to add a button in first element of the list to "Add new item". If user clicks on that button I need to open a pop-up and get the input from users. How to do this in select2 plugin? Is any default options for this (or) need to customize this?

Answer

user2976753 picture user2976753 · Jul 19, 2017

Here's my approach

$('#select2')
    .select2()
    .on('select2:open', () => {
        $(".select2-results:not(:has(a))").append('<a href="#" style="padding: 6px;height: 20px;display: inline-table;">Create new item</a>');
})

enter image description here