0

How to save div title into cookies? And how to get this saved data, back?

<script type="text/javascript">
function setcookie(title, value, exp) {
    var expdate = new Date();
    expdate.setDate(expdate.getDate() + exp);
    document.cookie = title+'='+value+';expires='+expdate.toGMTString()+';path=/';
}
</script>

<div title="1" onclick="setcookie();"></div>
Annika Backstrom
  • 13,337
  • 5
  • 38
  • 52
Nation
  • 11
  • 3
  • 8

2 Answers2

0

See this question about jQuery and cookies. It will be easier to use a plug-in, like http://plugins.jquery.com/project/cookie.

Use following code to get the title of the div:

title = $("div").attr("title");
Community
  • 1
  • 1
kgiannakakis
  • 96,871
  • 26
  • 155
  • 191
0

You may want to look into window.localStorage. It's very effective for what you are looking to do.

//Save the data
window.localStorage['mydiv'] = $('div').attr('title');

//Retrieve the data
$('div').attr('title', window.localStorage['mydiv']);
John Strickler
  • 23,772
  • 4
  • 49
  • 67
  • As PoweRoy says, localstorage only works with the most up-to-date browsers. It's a great feature, but you need to check that your target audience will be able to use it before you commit to anything that new. – Spudley Sep 29 '10 at 13:16