views:

56

answers:

2

Hi, 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>
A: 

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");
kgiannakakis
The link seems to be outdated! :(
Nation
@Nation I'm using it too. Pasted it on pastebin here: http://pastebin.com/GFvUTDV2
Arnis L.
Tnx, but how to make it work for div title?
Nation
@Nation: See my edited answer.
kgiannakakis
With jQuery, you can grab the title attribute on a click event.
Chouchenos
A: 

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
Note: HTML5 only
PoweRoy
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