Solved

jQuery data cache question

Posted on 2008-10-23
1
1,021 Views
Last Modified: 2012-05-05
I noticed in the jQuery documentation, there's a "data cache" with the function calls data(name) and data(name, value). Does anybody know if this data cache persists across loading multiple different pages? And if it doesn't, what is it good for?

Thanks
0
Comment
Question by:tjgquicken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
sh0e earned 50 total points
ID: 22792811
They do not persist, they are attached to the DOM object.
They are "custom properties" that you attach to the DOM node.
So, if you remove the DOM element the data store goes with it.

It can be useful if you want to store data that you want to associate with an element, and can help with scoping for example (to keep things in the scope of the element).

Below script checks if I pressed the same key as last time.  Notice how using data allows me to stay within the scope of the element.  There are other uses too of course, and it's used in plugins a lot.
<div id="mydiv"></div>
<script>
$('mydiv').keyup(function(event){
  if( $(this).data('lastclick') == event.keyCode )
    alert('same as last time');
  $(this).data('lastclick', event.keyCode);
});
</script>
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question