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

167
Views
JS TypeError while trying to change a color of progressBar

html

<div class="progress-bar" id="progressBar"></div>

script

<script>
  function progressBar() {
    let scroll = document.body.scrollTop || document.documentElement.scrollTop;
    let height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
    let scrolled = scroll / height * 100;
    document.getElementById('progressBar').style.width = scrolled + '%';
    console.log(scrolled);
    if (scrolled == 100);
      progressBar.style.backgroundColor = "green";
}
  window.addEventListener('scroll', progressBar);
  </script>

I need to change the color of the progressBar from red to green when i scroll to the bottom, but for some reason i get TypeError when i try to change background color

Uncaught TypeError: Cannot set properties of undefined (setting 'backgroundColor')
    at progressBar (index.html:144)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Several issues

  1. Do not use the same name for a function and a variable
  2. You did not post the CSS, it must be position fixed to work
  3. You had a semicolon after the if to set the bar green

const bar = document.getElementById('progressBar');

function progressBar() {
  let scroll = document.body.scrollTop || document.documentElement.scrollTop;
  let height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
  let scrolled = scroll / height * 100;
  bar.style.width = scrolled + '%';
  if (scrolled == 100) bar.style.backgroundColor = "green";
}
window.addEventListener('scroll', progressBar);
.progress-bar {
  background-color: red;
  height: 20px;
  width: 0px;
  position: fixed;
}
<div class="progress-bar" id="progressBar"></div>
<p>X<br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<p><br />X</p>

about 4 years ago · Juan Pablo Isaza Report

0

Wrong here : if (scrolled == 100);,remove the semicolon.

Also here progressBar.style.backgroundColor = "green"; progressBar here is the function not the progress Bar element (DOM element), and also you shouldn't do this document.getElementById('progressBar').style.width = scrolled + '%'; instead assign the progress bar element to a variable (variable's name SHOULD NOT be same as the function) and then use it inside the function like this:

<script>
      let progressBarElement = document.getElementById("progressBar");
      function progressBarFunction() {
        let scroll =
          document.body.scrollTop || document.documentElement.scrollTop;
        let height =
          document.documentElement.scrollHeight -
          document.documentElement.clientHeight;
        let scrolled = Math.floor((scroll / height) * 100);

        progressBarElement.style.width = scrolled + "%";
        if (scrolled >= 100)
          progressBarElement.style.backgroundColor = "green";

        console.log(scrolled);
      }
      window.addEventListener("scroll", progressBarFunction);
</script>

The style for the progressBar:

<style>
   #progressBar {
        position: fixed;
        height: 5px;
        background-color: orange;
   }
</style>
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!