?
Solved

jQuery data cache question

Posted on 2008-10-23
1
Medium Priority
?
1,023 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 200 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

752 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