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

147
Views
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 answers
Answer question

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 Report
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!