CKTIS / JavaScript
HTMLProgressElement: max and value properties
`max` sets the total amount of work and `value` sets the amount done. A missing `value` attribute means indeterminate progress, not 0% complete.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const p = document.createElement("progress");
p.max = total;
p.value = completed;
const fraction = p.position;
p.removeAttribute("value");HTMLProgressElement.max/value refers to two writable numeric DOM properties of a <progress> element, not ECMAScript functions. max is the total amount of work and value is the amount done. Use the same unit for both, such as bytes. The lower bound is always 0; a min attribute cannot change it.
Reading p.max returns a positive number, or 1 if the max attribute is missing, cannot be parsed as a number, or contains a nonpositive number. Assigning a positive number to p.max sets the attribute; the standard ignores finite nonpositive assignments. Assigning NaN, Infinity, or -Infinity to either property throws a TypeError.
Assigning a finite number to p.value sets the value attribute. Its presence makes progress determinate: on read, a negative or unparseable attribute value gives 0, and a value above p.max gives p.max. This clamping does not rewrite the attribute text returned by p.getAttribute("value").
Without the value attribute, progress is indeterminate even though p.value still returns 0. Assigning p.value = 0 means determinate progress at 0%; p.removeAttribute("value") makes it indeterminate again. The read-only p.position returns -1 in that state and p.value / p.max when progress is determinate.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTMLProgressElement: max and value</title>
</head>
<body>
<script>
const p = document.createElement("progress");
console.log(`initial: max=${p.max}, value=${p.value}, position=${p.position}`);
p.max = 8;
p.value = 2;
console.log(`partial: value=${p.value}, position=${p.position}`);
p.value = 10;
console.log(`over: attribute=${p.getAttribute("value")}, value=${p.value}, position=${p.position}`);
p.removeAttribute("value");
console.log(`removed: max=${p.max}, value=${p.value}, position=${p.position}`);
</script>
</body>
</html>Result
initial: max=1, value=0, position=-1 partial: value=2, position=0.25 over: attribute=10, value=8, position=1 removed: max=8, value=0, position=-1
Save the example as an HTML file, open it in a browser, and check the output in the developer console. The `over` line shows that the attribute keeps the text `10` while the property returns `8`. The element is not inserted into the page.
Related lessons
- JavaScript · Lesson 6 · Advanced · Version: 4