Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda