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

197
Views
Javascript to add or remove classes (hover effect)

Topics, Specialist, Text I have segments that look like little cards with words in them. I would like to unhide a description for the specific word when the user hovers over them.

The code I have is as follows with the concerned classes being "specialist", "specialist-text":

const spec = document.querySelector('.specialist')
const spect = document.querySelector('.specialist-text')

spec.addEventListener('mouseenter', () => spect.classList.remove('hidden'))
.hidden {
  display: none;
}
<section class="topics">
        <h3 class="large center my-1">Topics</h3>
        <div class="width grid overflow">
             <div class="topic-list-update">
                <div class="card2 center btn2 specialist">Specialist</div>
             </div>
             <div class="topics-text">
                <div class="specialist-text hidden">Specialist mathematics 1234</div>
             </div>
        </div>
</section>

And I don't understand why it's not working. I attempted with addEventListener('mouseover'), I have tried using document.getElementById and I tried to use a function instead of an arrow function.

I am receiving an error when launching the html file in firefox:

Uncaught TypeError: spec is null
    <anonymous> http://127.0.0.1:5500/js/script.js:12

I am a complete beginner and I thank you in advance for your help.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

The answer was to load the script at the very end of the body!

<body>
    <div> What ever 1 2 3 4 <br/> 1 2 3 4</div>
    <img>
    <script src="script.js"></script>
</body>
about 4 years ago · Santiago Gelvez 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!