Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Data Registry

If for some reason you're not yet familiar with data(), I recommend re-reading the Data Attributes chapter and checking out the official documentation.

In the context of plugin development, here's what we need to remember — the $.data() method lets you maintain a data registry tied to a specific element. So it's best to store all plugin data in this registry. If you need to save the plugin state — use data(), if you need a cache — use data(), if you need to save ... well, I think you get the picture. Here's another little example related to initialization:

function() { // init function
    var init = $(this).data('mySimplePlugin');
    if (init) {
        return this;
    } else {
        $(this).data('mySimplePlugin', true);
        return this.on('click.mySimplePlugin', function(){
            $(this).css('color', options.color);
        });
    }
}

I should remember to tell you about obj[jQuery.expando] = uuid and jQuery._data(element), and also about memory leaks, but probably not today.

Last updated