2

Get custom data-attribute in select2 with <select>

We need the same thing specified in the above link. But, how to get the attributes of option element in formatResult function?

Community
  • 1
  • 1
CSR
  • 668
  • 12
  • 29

1 Answers1

2

You could try the following code :

  $select2.data('placeholder', 'please choose').html(_options).select2({
                    formatNoMatches: function (term) {
                        return 'There is no "' + term + '" item';
                    },
                    formatResult: function(term) {
                        return $(term.element).data('cnt');
                    },
                    allowClear: true
  });

JSBIN

holmes2136
  • 481
  • 5
  • 14