Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $(this) in select2

I'm trying to read a collection from the html5 data-attribute of the input that is converted to select2 to create tags.

This is working when I have one input:

$(".tags").select2(
  width: '220px'
  tags: $(".tags").data('collection')
)

But I will like to do it more safe using the data of the element itself, I tried this:

$(".tags").select2(
  width: '220px'
  tags: $(this).data('collection')
)

But it fails with the error:

Uncaught query function not defined for Select2 investigador_aplicaciones

Do you know if it is posible to use the element itself with a specific selector like $(this)?

like image 786
Jorge Sampayo Avatar asked Jun 07 '26 11:06

Jorge Sampayo


1 Answers

You can do this instead:

$(".tags").each(function(){
  var $this = $(this);
  $this.select2({
  width: '220px',
  tags: $this.data('collection')
  });
});

Because during your call this doesn't represent the element in the selector.

like image 124
PSL Avatar answered Jun 10 '26 04:06

PSL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!