Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
How do I simply remove a class from body if cookie is present?

I have what I would think is a pretty simple code issue, but nonetheless. Basically, everything works as it should except all I need is to remove a class from the document body if a cookie is present. I have a cookie policy popup overlay that appears on first visit. When that is accepted a cookie is set. However, if the cookie is set I no longer want the side menu to be expanded, which is merely a class in the body called 'show-menu'.

I understand that all you need to do to remove a class from the body is: document.body.classList.remove('show-menu');

However, the only place in my cookie.js file that references checking if there is a cookie, and whether to display the overlay again or not is here:

        if (this.options.cookieAlert)
        {
            if (getCookie(this.options.cookieName) !== '')
            {
                this.destroy();
                return false;
            }

            if (this.options.cookieAcceptButtonId !== null)
            {
                document.getElementById(this.options.cookieAcceptButtonId).addEventListener('click', function(e){
                    e.preventDefault();
                    setCookie(that.options.cookieName, 'yes', that.options.cookieExpireDay);
                    that.destroy();
                });
            }
        }

I tried adding the classList.remove('show-menu') line above 'this.destroy();' to see if it would at least remove the 'show-menu' class from the body before it canceled the function.

And here is the 'getCookie' var code. I wasn't able to add the classList.remove line to this either and get it to work.

    var getCookie = function(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!