Created
October 17, 2011 12:51
-
-
Save kahlil/1292542 to your computer and use it in GitHub Desktop.
JavaScript: Storing data with an object. Equivalent with data()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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