Search code examples
javascripthtmljqueryjquery-plugins

Get jquery plugin options from data-* attributes?


Is it possible to do something like that:

<ul class="handle" data-option="9000">

$('.handle').pluginName({
    option: $('this').data('option'),
});

where $('this') should target the $('.handle') which is being initialized.


Solution

  • No, it isn't, you can loop through the elements:

    $('.handle').each(function() {
       var $this = $(this);
       $this.pluginName({ option: $this.data('option') });
    });
    

    Or if you are the plugin's author read the data-* attributes in your plugin.