By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – JavaScript progress bar unhides but won’t update
JavaScript

Javascript – JavaScript progress bar unhides but won’t update

Admin
Last updated: 2024/02/07 at 5:23 PM
Admin
Share
3 Min Read
JavaScript progress bar unhides but won't update

Problem:

I want a progress bar to unhide when the button is pressed and then to update. However, with the following code the progress bar shows but does not update.

Contents
Problem:Solution:

If I remove style="display: none" the bar updates as expected. Any ideas?

document.getElementById("submit-btn").addEventListener("click", function() {
  document.getElementById("progress-top").style.display = 'block';

  var i = 0;
  if (i == 0) {
    i = 1;
    var width = 1;
    var id = setInterval(frame, 50);

    function frame() {
      if (width >= 100) {
        clearInterval(id);
        i = 0;
      } else {
        width++;
        document.getElementsByClassName('progress-bar').item(0).setAttribute('aria-valuenow', width);
        document.getElementsByClassName('progress-bar').item(0).setAttribute('style', 'width:' + Number(width) + '%');
      }
    }
  }


})
<div class="progress" style="display: none" id="progress-top">
  <div class="progress-bar" role="progressbar" aria-valuenow="" aria-valuemin="0" aria-valuemax="100">
  </div>
</div>

I’m using Bootstrap 4 for the CSS

.progress {
  display: flex;
  height: $progress-height;
  overflow: hidden; // force rounded corners by cropping it
  font-size: $progress-font-size;
  background-color: $progress-bg;
  @include border-radius($progress-border-radius);
  @include box-shadow($progress-box-shadow);
}

.progress-bar {
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: $progress-bar-color;
  text-align: center;
  background-color: $progress-bar-bg;
  @include transition($progress-bar-transition);
}

Solution:

Consider using the <progress> element were you just need to se the value.

To prevent starting the loop twice, consider checking the PID.

let id = null;

document.getElementById("submit-btn").addEventListener("click", function() {

    document.getElementById("progress-top").style.display = 'block';
       
    if (id === null) {
        var val = 1;
        id = setInterval(frame, 50);

        function frame() {
            if (val >= 100) {
                clearInterval(id);
                id = null;
            } else {
                val++;
                document.getElementById('progress').setAttribute('value', val);
            }
        }
    }
})
.progress {
    height: 150px;
}
<div class="progress" style="display: none" id="progress-top">
    <progress id="progress" value="0" max="100">0%</progress>
</div>

<button id='submit-btn'>Click me</button>

Expand snippet

Regarding the Bootstap variant, you’ll need to set both style.width and innerHTML.

The aria-valuenow doesn’t need to be updated.

let id = null;

document.getElementById("submit-btn").addEventListener("click", function() {

    document.getElementById("progress-top").style.display = 'block';
       
    if (id === null) {
        var val = 1;
        id = setInterval(frame, 50);

        function frame() {
            if (val >= 100) {
                clearInterval(id);
                id = null;
            } else {
                val++;
                document.getElementsByClassName('progress-bar')[0].style.width = val + '%';
                document.getElementsByClassName('progress-bar')[0].innerHTML = val + '%';
            }
        }
    }
})
.progress {
    height: 150px;
    width: 200px;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="progress" id="progress-top" style="display: none">
  <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>

<button id='submit-btn'>Click me</button>

Expand snippet

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Prevent firestore onUpdate cloud functions when onSnapshot is used Javascript – Prevent firestore onUpdate cloud functions when onSnapshot is used
Next Article js webix border box not fully visible Javascript – js webix border box not fully visible
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?