Skip to content Skip to sidebar Skip to footer

Jquery: Store Additional/extra Data/info Inside A Jquery Object?

Is it possible and smart to store extra data inside a jQuery object? Right now I have objects that contain some data but these objects also have a visual representation of that dat

Solution 1:

You can use $.data() for this :)

For example:

$.data(element, 'varName', value);      //storevar value = $.data(element, 'varName'); //get

Or use the object method .data():

$("#ElementID").data('varName', value);      //storevar value = $("#ElementID").data('varName'); //get

This doesn't store the data on the object, rather it stores it in $.cache (try it in your console on this page), but it's associated with the object, via this[$.expando].

However if you call .empty() that removes an object, or .remove(), it'll do the cleanup for you. You can also call .removeData() or $.removeData() to remove it directly.

Post a Comment for "Jquery: Store Additional/extra Data/info Inside A Jquery Object?"