vendredi 4 mai 2018

Disable checkbox and keep it disabled using cookies

This will keep the checkbox checked:

 function setCookie(c_name,value,expiredays) {
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate)
}

function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1) { 
            c_start=c_start + c_name.length+1 
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
        } 
    }
    return null
}
onload=function(){
document.getElementById('myCheck').checked = getCookie('myCheck')==1? true : false;
}
function set_check(){
setCookie('myCheck', document.getElementById('myCheck').checked? 1 : 0, 100);

}

<input type="checkbox" value="1" id="myCheck">

I would also like to disable the checkbox after it's checked and keep it disabled using the cookies. How would I add that function to the existing code? Thanks!




Aucun commentaire:

Enregistrer un commentaire