Q:

Jquery - hide element after cookie is made

i have a hidden element which appears upon users scroll. When a text "Click to close" (located inside the element) is clicked then the element hiddes and sets a cookie, which prevents it's visibility upon refresh. My question: is it possible that the element stays hidden upon setting a cookie without refresh? Currently when text inside it is clicked element hides but then it shows up again upon scroll.
My current setup: fiddle.
Thanks.

A:

If you want to do this solely by testing for the cookie, you should move the cookie check conditional into your scroll event handler. Right now, you're binding the scroll event on page load if the cookie doesn't exist, but once it is set, you're not unbinding the scroll event. See updated fiddle.
$(document).scroll(function() {
    if (document.cookie.indexOf("testCookie") < 0) {
        var y = $(this).scrollTop();
        if (y > 300) {
            $("#test").show();
        }
    }
});

