younes0
younes0

Reputation: 2302

Jquery plugin: accessing element attributes inner the settings init

I'm trying to access a data attribute ('maxselection') of several html elements while attaching them to a jquery plugin :

$(".select2").select2({ // several html elements
    maximumSelectionSize: $(this).data('maxselection')
});

That doesn't work.

How can you reach the element attributes in this specific scope ?

btw: select2 is a dropdown enhancer, a fork of chosen

Upvotes: 0

Views: 121

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

this is not the selected element(s) inside of the object literal definition. Something like this would work:

$(".select2").each(function () {
    var $this = $(this);

    $this.select2({
        maximumSelectionSize: $this.data('maxselection');
    });
});

Upvotes: 1

Related Questions