Progress Bar in JavaScript
Update a five-chapter tracker in JavaScript with a native progress element and a percentage. Clamp the count at five; clicks are not proof of reading.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<label for="progress">Reading progress</label><progress id="progress" max="5" value="0"></progress><p id="status">0 / 5 chapters (0%)</p><button id="next">Finish a chapter</button><button id="reset">Reset</button>7<script>8const max = 5;9let value = 0;10const progress = document.querySelector('#progress');11function render() {12 value = Math.max(0, Math.min(max, value));13 progress.value = value;14 document.querySelector('#status').textContent = value + ' / ' + max + ' chapters (' + Math.round(value / max * 100) + '%)';15 document.querySelector('#next').disabled = value === max;16}17document.querySelector('#next').onclick = () => { value++; render(); };18document.querySelector('#reset').onclick = () => { value = 0; render(); };19render();20</script>21</body>22</html>
Sample Output
2 / 5 chapters (40%)
Two divided by five, multiplied by 100, gives 40 percent.
5 / 5 chapters (100%)
The count reached its maximum and the completion button is disabled.
In this one
- Connect a count to a percentage
- Use the native progress element
- Clamp values to the valid range
- Disable completion beyond the total
How it works
- 1
Set a positive total of five chapters.
- 2
Increase the completed count on each button click.
- 3
Clamp the count between zero and five.
- 4
Set the progress value and format the percentage.
- 5
Two clicks show 40%; five clicks show 100% and disable further increments.
Edge cases
Zero total
Dividing by max zero does not produce a useful percentage.
Validate that the total is positive if it comes from input. For unknown work, do not display a made-up percentage.
Count above total
Repeated changes could otherwise exceed 100 percent.
Clamp the count and disable the increment button at completion.
What's going on
A progress bar shows how much of a known task is complete. This reading tracker counts five chapters, so the filled part of the bar has a clear meaning rather than being a decorative animation.
value holds completed chapters and max is the total. The percentage is value divided by max, multiplied by 100. render limits value to the valid range before updating the native progress element and the text beside it. A native element is one the browser already understands and provides behavior for.
The Finish a chapter button increases the count once per click. At five chapters it is disabled, and Reset returns the count to zero. This is a local learning tracker; clicking is only a manual claim of completion, not proof that a chapter was read. For a task with an unknown total, use an indeterminate progress indicator instead of inventing a percentage.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is three out of five as a percentage?
Pick an answer to check it.
Question 2
What should an unknown total use?
Pick an answer to check it.
Questions people ask
Why use progress instead of a colored div?
The browser already knows this element represents progress, making a basic example easier to understand and label.
Is 40% two chapters out of five?
Yes. 2 divided by 5 is 0.4, which becomes 40 after multiplying by 100.
Will completion survive refresh?
No. This page keeps the count in memory only.