Button at the end of the Select2 drop down list

1,875 views
Skip to first unread message

rec...@gmail.com

unread,
Feb 11, 2016, 9:36:26 AM2/11/16
to select2
I am using the lastest version of Select2. Want to add an Insert New button at the end of the dropdown list. I tried these two solutions I found online

Solution 1
$(".select2-drop").append('<table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>');

Solution 2
$("#itemId0").select2("container").find("div.select2-drop").append('<table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>');

With Solution 1 nothing happens. With solution 2 I get the error message in the select 2 js file
0x800a138f - JavaScript runtime error: Unable to get property 'apply' of undefined or null reference

Can anyone please help?

Thank you very much

Ayala Levinger

unread,
Apr 8, 2016, 5:00:52 AM4/8/16
to select2, rec...@gmail.com
If you need the button only when there are no results:

$('.select2').select2({
       allowClear: true,
    
         language: {
             noResults: function() {
                  return '<

table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>

';
            }
         },
       

        escapeMarkup: function (markup) {
            return markup;
        }
    });
Reply all
Reply to author
Forward
0 new messages