Solved

jQuery data cache question

Posted on 2008-10-23
1
979 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
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
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 invoke it on a web page. 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.: (CODE)

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now