Stackoverflow Response: How to add object to jQuery object such that I can retrieve it later?

http://stackoverflow.com/questions/16241213/how-to-add-object-to-jquery-object-such-that-i-can-retrieve-it-later/16241547#16241547

by Terry Young

HTML

<a href="http://stackoverflow.com/questions/16241213/how-to-add-object-to-jquery-object-such-that-i-can-retrieve-it-later/16241547#16241547" target="_blank">Stackoverflow Response: How to add object to jQuery object such that I can retrieve it later?</a>


<div id="start_date"></div>

JavaScript

// Small plugin
    $.fn.myData = function (namespace) {
        var namespaces = namespace.split('.'),
            data = this.data();
        $.each(namespaces, function (i, that) {
            data = data[that];
        });
        return data;
    }
    
    
    
    // set your data
    $('#start_date').data('dateTime', {
        bacon:5, 
        myFunc: function() { 
            alert('mmm, bacon') 
        }
    });
    
    
    // call your function, note the dot notation
    $('#start_date').myData('dateTime.myFunc')(); // better?