Solved

jQuery data cache question

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery 7 54
replacing inline javascript with jquery 4 63
Difference between Leaflet and MapBox? 5 45
jquery autocomplete forcing the user to select an item from the list 7 63
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

732 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