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

301
Views
How do I use the document.getElementsByClassName().style to change the color of text?

So I tried to make a Light Mode function. But to do that, I need to change the color of some elements, but I have assigned classes to them. Someone tell me how to use the document.getElementsByClassName() function to change the color of elements assigned that class?

I tried doing this (see second code snippet) but it only effects 1 Element. I have 2 of the same element with the same class on a page.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>title</title>
  <style>
        .myH1{
           color:black;
</head>

<body>
<script>
function lightMode(){
      document.body.style.background = "white";
      document.getElementsByClassName("myH1").style.color="blue";
</script>
      
  <h1 class="myH1">Header</h1>
  <button onclick="lightMode()">☀</button>
  
</body>

</html>
function lightMode()
       document.body.style.background="white";
       const element = document.getElementsByClassName("myH1")
       element.style.color="black";
about 4 years ago · Juan Pablo Isaza
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!