Usually I donβt need to include the jQuery library in my scripts, however, I just recently came across jQuery $ .data () functions that can bind data in the form of pairs of key values, objects and even functions to any element,
From what I read, the jQuery $ .data () function also has built-in precautions that prevent memory leaks associated with such methods, but it overflows to include the entire JQ library for this single function.
Does anyone know of a native alternative?
EDIT To make it clearer, I'm not looking for a native function to retrieve the attributes of an element. The jQuery $ .data () API goes far beyond such use, expanding its ability to associate javascript objects and functions with jQuery element nodes.
This article ( http://tutorialzine.com/2010/11/jquery-data-method/ ) addresses this use, but as an example I use it to associate a GSAP Timeline animation with an object so that I can access and call the GSAP Timeline.reverse () function of the animation outside the function that it created. For instance:
function doAnimation(){ var element = document.createElement('div'), timeline = new TimelineMax({....GSAP related fns...}), options = { .... timeline: timeline }; $(element).data('options', options); } function reverseAnimation($element){ var options = $element.data('options'), previouslyCreatedTimeline = options.timeline; previouslyCreatedTimeline.reverse(); }
It may not be the most vivid example if you are not a GSAP user, but in fact, the $ .data () method allowed me to associate a javascript object with an element so that I could access its methods in a function outside its original scope.
source share