0

I am trying to have it look to see if the cookie exists and if not fade in. Then if the user clicks the close button it fades out. What am I doing wrong?

Jquery

$(function() { if ($.cookie("demoCookie") == null) { $("#headerFactInfo").fadeIn(); }; $("#headerFactInfoClose").click(function() { $("#headerFactInfo").fadeOut(); $.cookie( 'demoCookie', '1', { expires: 7, path: '/' } ); }); }); 

Html

<div id="headerFactInfo">The Great Add <a href="" id="headerFactInfoClose" >Close</a></div> 
3
  • What's the problem? What's happening or not happening? Commented Jan 4, 2012 at 5:37
  • Non of it is working, it wont set the cookie, it wont fade in and as far as I can tell its not reading the cookie. Commented Jan 4, 2012 at 5:41
  • Do you get any error messages in your JS console? And, perhaps this is obvious but just in case: did you remember to include the JS file for the cookie plugin (it's not a core part of jQuery)? Commented Jan 4, 2012 at 5:56

2 Answers 2

1

Your code seems to work fine: http://jsfiddle.net/wPP6Y/

I made only 2 modifications:

CSS

/* It's hard to tell whether it's fading in * or not if it's always visible */ #headerFactInfo { display: none; } 

JS

$("#headerFactInfoClose").click(function() { $("#headerFactInfo").fadeOut(); $.cookie( 'demoCookie', '1', { expires: 7, path: '/' } ); return false; // Prevent the link to actually follow the href }); 

Use Firebug for Firefox or Chrome's developer tools, or your favorite browser's equivalent to play around with the cookie.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I forgot to put return false;
0

If you mean, cookie check is not working then, try:

  $(function() { var Cooki = $.cookie('demoCookie'); if (Cooki == null) { $("#headerFactInfo").fadeIn(); } $("#headerFactInfoClose").click(function() { $("#headerFactInfo").fadeOut(); $.cookie( 'demoCookie', '1', { expires: 7, path: '/' } ); }); });  

2 Comments

I'm curious why you think storing the function return in a variable before the comparison would make a difference? (Or is there some other change in your code that I'm not seeing?)
As of right now non of it works, it does not fade in, it does not create the cookie and as far as I can tell it does not read if there is a cookie but this could be just because I cant create a cookie. So I dunno what I am doing wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.