title: Vanilla alternative to jQuery $.data() function: any native javascript alternative?
body: <p>I generally avoid having to include the jQuery library in my scripts, however I only recently came across jQuery $.data()'s functions ability to associate data, in the form of key value pairs, objects &amp; even functions, with any element. </p>
  
  <p>From what I've read, jQuery's $.data() function also has built in safeguards that prevent memory leakage associated with such practices, but it's overkill to include the entire JQ library for that single function.</p>
  
  <p>Does anyone know of a native alternative?</p>
  
  <p><strong>EDIT</strong> 
  To make myself more clear, I'm not seeking the native function to retrieve element attributes. jQuery's $.data() API goes far beyond such use, extending to its ability to associate javascript objects &amp; functions with jQuery element nodes. </p>
  
  <p>This article (<a href="http://tutorialzine.com/2010/11/jquery-data-method/" rel="noreferrer">http://tutorialzine.com/2010/11/jquery-data-method/</a>) touches on this usage, but as an example I currently am using it to associate a GSAP Timeline animation with an object, so that I can access and call GSAP Timeline's .reverse() animation function outside of the function which it is created. For example:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>Maybe not the clearest example if you aren't a user of GSAP, but in essence, the $.data() method allowed me to associate a javascript object with an element, so that I can access it's methods in a function outside of its original scope.</p>