Skip to content

Instantly share code, notes, and snippets.

@kahlil
Created October 17, 2011 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kahlil/1292542 to your computer and use it in GitHub Desktop.
Save kahlil/1292542 to your computer and use it in GitHub Desktop.
JavaScript: Storing data with an object. Equivalent with data()
// Found here: http://stackoverflow.com/questions/7783907/equivalent-of-data-for-jquery-objects/7784011#7784011
$.fn.objData = function(key, value) {
var rootData = $(document).data("jQueryObjectData");
if (!rootData) {
$(document).data("jQueryObjectData", rootData = {});
}
var objData = rootData[this];
if (!objData) {
rootData[this] = objData = {};
}
if (typeof value === "undefined") {
return objData[key];
}
objData[key] = value;
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment