Skip to content

Commit ec6bddd

Browse files
committed
Add example of showing dynamic range value with output
1 parent 13aa16a commit ec6bddd

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

site/src/content/docs/forms/range.mdx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toc: true
66

77
## Overview
88

9-
Create custom `<input type="range">` controls with `.form-range`. The track (the background) and thumb (the value) are both styled to appear the same across browsers. As only Firefox supports filling their track from the left or right of the thumb as a means to visually indicate progress, we do not currently support it.
9+
Create custom `<input type="range">` controls with `.form-range`. The track (the background) and thumb (the value) are both styled to appear the same across browsers. As only Firefox supports "filling" their track from the left or right of the thumb as a means to visually indicate progress, we do not currently support it.
1010

1111
<Example code={`<label for="customRange1" class="form-label">Example range</label>
1212
<input type="range" class="form-range" id="customRange1">`} />
@@ -27,11 +27,32 @@ Range inputs have implicit values for `min` and `max`—`0` and `100`, respectiv
2727

2828
## Steps
2929

30-
By default, range inputs snap to integer values. To change this, you can specify a `step` value. In the example below, we double the number of steps by using `step="0.5"`.
30+
By default, range inputs "snap" to integer values. To change this, you can specify a `step` value. In the example below, we double the number of steps by using `step="0.5"`.
3131

3232
<Example code={`<label for="customRange3" class="form-label">Example range</label>
3333
<input type="range" class="form-range" min="0" max="5" step="0.5" id="customRange3">`} />
3434

35+
## Output value
36+
37+
The value of the range input can be shown using the `output` element and a bit of JavaScript.
38+
39+
<Example code={`<label for="customRange4" class="form-label">Example range</label>
40+
<input type="range" class="form-range" min="0" max="100" value="50" id="customRange4">
41+
<output for="customRange4" id="rangeValue"></output>
42+
43+
<script>
44+
// This is an example script, please modify as needed
45+
const rangeInput = document.getElementById('customRange4');
46+
const rangeOutput = document.getElementById('rangeValue');
47+
48+
// Set initial value
49+
rangeOutput.textContent = rangeInput.value;
50+
51+
rangeInput.addEventListener('input', function() {
52+
rangeOutput.textContent = this.value;
53+
});
54+
</script>`} />
55+
3556
## CSS
3657

3758
### Sass variables

0 commit comments

Comments
 (0)