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
Solved

jQuery data cache question

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
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…

856 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