0

I have found similar question with solution here: Get custom data-attribute in select2 with <select>

I have placed custom data attribute with each option.

<option value="pen" data-price="2.5">Pen</option>

When I applied the accepted solution of that question,

var price = $(this).select2().find(':selected').data('price');

I get "Cannot read property 'current' of null".

Here is my fiddle demo

user1896653
  • 3,247
  • 14
  • 49
  • 93

2 Answers2

1

Try this

$('.select-multiple').on('select2:select',function (e) {
   var price = $(e.params.data.element).data('price');
   console.log(price);
});

Fiddle

Sijan Bhattarai
  • 570
  • 2
  • 6
  • 25
1

Your price variable is iterable. You have to iterate all selected options.

Try this:

    $('.select-multiple').change(function () {
      var price = $(this).find(':selected');

      $.each(price, function(){
          console.log('Price: ', $(this).data("price"));
      });
    });

JSFiddle

Ogreucha
  • 633
  • 1
  • 5
  • 24