-1

I need a jQuery code, not JavaScript, that will delete cookies and set elements to show again.

Here is my code:

HTML:

<div class="recoverit"><a href="javascript:;">Recover Cookies</a></div>

jQuery:

document.cookie = elementId+ "=true; path=/; expires=Th, 31 Dec 2099 11:00:00 GMT;" + date.toUTCString();   

Storing of the cookies works great, but now I need to remove them and "elementId" to show again on the same places.

Thanks.

Scott
  • 1,863
  • 2
  • 24
  • 43
stormec
  • 179
  • 1
  • 10

1 Answers1

0

You can try this:

function delete_cookie(elementId) {
    document.cookie = elementId + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};

Or you can use jquery as others suggested.

UPDATE full sample as asked:

<html>
<script>
function setCookie(elementId) {
    document.cookie = elementId + "=true; path=/; expires=Th, 31 Dec 2099 11:00:00 GMT;";
    document.getElementById(elementId).style.display = "none";
}

function deleteCookie(elementId) {
    document.cookie = elementId + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    document.getElementById(elementId).style.display = "block";
}
</script>
<body>
<a href="#" onclick="setCookie('el1');">hide element</a>

<a href="#" onclick="deleteCookie('el1');">restore element</a>

<br>
<div id="el1">one two three...</div>
</body>
</html>
Jkike
  • 807
  • 6
  • 12
  • I don't want to use any plugins.. I will try this, but how do I set the element to show again without refreshing? – stormec Aug 27 '15 at 15:02
  • he asked for a jquery soloution – ThunD3eR Aug 27 '15 at 15:02
  • document.getElementById(elementId).style.display='block' no jquery $('#elementId').show() jquery – Jkike Aug 27 '15 at 15:04
  • @Ra3IDeN but it does not want a plugin either. Downgrading my answer is not correct, since it works even if you have jquery. – Jkike Aug 27 '15 at 15:06
  • Jkike, could you please elaborate more? Thanks. – stormec Aug 27 '15 at 15:16
  • I don't understand what do I have to elaborate... you ask how to show an element again, so I rekon you have it hidden at the moment. The reverse of hidden is document.getElementById(elementId).style.display='block' or $('#elementId').show() where elementId is the id of the html element you want to show. Of course it must exist in the page, although is hidden. – Jkike Aug 27 '15 at 15:20
  • Okay, I will let you know if it works and mark your response as correct. Thanks once more. – stormec Aug 27 '15 at 15:23
  • Is there a chance you can write the entire code for this, for some reason it's not working. – stormec Aug 28 '15 at 08:52