Pregunta

I'm pretty new to javascript, so I'm hoping someone can help me out with this. I'm using the javascipt cookie file here: https://github.com/carhartl/jquery-cookie, and trying to set a different cookie with different expiration dates depending on if a tick box is ticked or not. Basically, if the box is ticked, it sets a cookie with a 7 day expiration date, if it's not ticked, it sets a cookie that expires after 1 day. This is what I've got so far, but it's not setting the cookie at all.

Thanks.

$(document).ready(function() {

//check if modal cookie is present

    if ($.cookie('modal_shown') == null) {

//check if checkbox is checked
//if so, set the cookie to expire after 7 days

        if ($('#checkbox1').prop('checked')) {
            $.cookie('modal_shown', 'yes', { expires: 7, path: '/' });
            }

//if not checked, set the cookie to expire after 1 day

        else {
            $.cookie('modal_shown', 'yes', { expires: 1, path: '/' });
            }
        $('#thisModal').reveal();
    }
});
¿Fue útil?

Solución

$.cookie('modal_shown', 'yes', { expires: ($('#checkbox1').prop('checked') ? 7 : 1), path: '/' });

This is using a shorthand if statement to determine whether the checkbox is checked or not

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top