1

I'm looking to use cookies to disable a button after one press using Js-Cookie for a quiz

start_btn.onclick = ()=>{
    info_box.classList.add("activeInfo"); 
}

I'm not sure whether I should reference the button or edit its function?

so say I was to do

 start_btn.onclick = ()=>{
    info_box.classList.add("activeInfo"); 
    Cookies.set('completedquiz')
}

how would i then search for the cookie before loading the rest of the quiz I've looked a lot on YouTube and haven't found much along with the page itself https://github.com/js-cookie/js-cookie but couldn't find what I was looking for

I now have

if( Cookies.get('completedquiz') ) {
start_btn.onclick = ()=>{
    returnfalse 
}
} else {
start_btn.onclick = ()=>{
    info_box.classList.add("activeInfo"); 
    Cookies.set('completedquiz', { expires: 7 })
}

but it just does the else whether I have the cookie or not

Got it

if( Cookies.get('completedquiz') == 'true') {
start_btn.onclick = ()=>{
    return false 
}
} else {
start_btn.onclick = ()=>{
    info_box.classList.add("activeInfo"); 
    Cookies.set('completedquiz', 'true', { expires: 7 })
}

}

1 Answers1

0

I suppose the answer would depend on how you're loading the rest of the quiz.

If you're loading it via Javascript you'd likely need to do the following:

if( Cookies.get('completedquiz') ) {
    // Don't load quiz
} else {
    // Load rest of quiz 
}

If you're loading the quiz via PHP you'd do the following:

if($_GET['completedquiz']) :
    // Don't load quiz
else :
    // Load rest of quiz
endif;

Reading the contents of the Cookie is covered in the documentation under Basic Usage > Read: https://github.com/js-cookie/js-cookie

Hope that helps!

Adam
  • 11
  • 4