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

156
Visualizações
Cannot get variable with query selection

I have written a small JS function to change the class name of an HTML element. Sadly, my const is somehow undefined, so the function doesn't run.

This is my JS:

deglitcher();

function deglitcher() {
    const glitch = document.querySelector('#bottom-gl');
    setTimeout(function () {
        glitch.className = 'glitch-off';
    }, 2000);
}

This is how my HTML looks like:

<div class="sign">
        <h1 class="glitch" id="upper-gl" data-text="Glitch1">Glitch1</h1> &nbsp; 
        <h1 class="glitch" id="bottom-gl"data-text="Glitch2">Glitch2</h1>
        <span class="sub">Test</span>
      </div>

I would like it to look like that:

<div class="sign">
        <h1 class="glitch-off" id="upper-gl" data-text="Glitch1">Glitch1</h1> &nbsp; 
        <h1 class="glitch-off" id="bottom-gl"data-text="Glitch2">Glitch2</h1>
        <span class="sub">Test</span>
      </div>

I hope someone smarter than myself, can figure it out

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The fix which I came up with, thanks to the generous help of ARMAAN and A Haworth, is as follows:

  1. Correct the code for bad selector ('bottom-gl) instead of ('#bottom-gl')
  2. Use classList instead of className
    deglitcher();
    
    function deglitcher() {
        const glitch = document.getElementById('bottom-gl');
        setTimeout(function () {
            console.log(glitch)
            glitch.classList.add('glitch-off');
            glitch.classList.remove('glitch'); 
            console.log('Trele Morele');
        }, 2000);
    }
  1. And finally - thanks to A Haworth, I used defer in my loader:
    <script src="../src/js/glitch.js" defer></script>

As of now the issue is resolved, thanks for your help everyone ;-)

about 4 years ago · Juan Pablo Isaza Relatório
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