Select2 formatResult get option attributes

Get custom data attribute in select2 using <select>

We need the same as the link above. But how do you get the attributes of the option element in the formatResult function?

+1


source to share


1 answer


You can 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

+2


source







All Articles