Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda