jQuery cookie

Try to remove that line at the very beginning of your code:

$j(".alert-box:first").show();

That line shows first alert box. And because it is placed at the beginning first alert shows immediately. But we want to assign IDs to each of alerts before showing any of them (do you remember?). Code that assigns IDs also contains command to show first alert, so we don’t need to run that command at the beginning of the script.

See how the whole thing looks now.

// Alert session cookie starts here 
jg(document).ready(function(){

jg('.alert-box').attr('id', function(i) {
   return 'alert'+(i+1);
});
     function closeAlert(){          
    var alert = jg('.alert-box:visible');
    var alertId = alert.attr('id');
    alert.remove();
    jg.cookie('Alert-is-closed-'+alertId, true, {path: '/'});
    //cookie name will be different for each alert:
    // Alert-is-closed-0
    // Alert-is-closed-1
    // Alert-is-closed-2
    // ...and so on...
}

if(jg.cookie('Alert-is-closed-' + jg('.alert-box:visible').attr('id'))){
        console.log('Closing the alert box automatically.');
        closeAlert();
    }

    
        // Close the box on click
    jg('.alert-switch').click(function () {
           
           closeAlert();
        
    });

    });

And? Does it work now?

Nope, Acts the same way. hides and shows the difference is that it ID"s the alerts.

Did you know that when we try and investigate your code, we have to try and create some HTML code (that is no doubt different from yours) to stop errors from happening? It would be great if we didn’t have to do that, which will also ensure that the code that we test will be behaving the same for as as it does for you.

1 Like

I totally understand that but the problem is that I do not know how to call the cookie file. I will try something this morning.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.