SyntaxStudy
Sign Up
jQuery Plugin Options via .data()
jQuery Advanced 4 min read

Plugin Options via .data()

Plugin Options Pattern

jQuery plugins often read their configuration from data-* attributes, allowing per-element customization without extra JavaScript.

Example
// Plugin reads options from element data
$.fn.tooltip = function(defaults) {
  return this.each(function() {
    const opts = $.extend({}, defaults, $(this).data());
    // opts.placement, opts.delay, opts.trigger from data attrs
    initTooltip(this, opts);
  });
};

// Usage in HTML — no extra JS needed
// <button data-placement="top" data-delay="500">Hover me</button>
$("[data-placement]").tooltip({ trigger: "hover" });
Pro Tip

$.extend({}, defaults, $(this).data()) merges defaults with per-element overrides.