A range input gives you a slider that constrains a number between a minimum and a maximum, but the control itself doesn't reveal which number will be submitted. The HTML spec is explicit about this: the input "represents a control for setting the element's value to a string representing a number, but with the caveat that the exact value is not important," so user agents are free to render a simpler interface than the Number state. For qualitative questions — "left for sad, right for happy" — that's fine. For most other uses it isn't, and nothing stops you from displaying the value yourself.

An <output> element, which exists precisely to hold values calculated from form elements, is the natural home for it. The goal is a bubble that tracks the current value of any range input, for any combination of min, max and step.

<input type="range" name="foo">
<output for="foo" onforminput="value = foo.valueAsNumber;"></output>

Positioning the Bubble

Writing the range's value into the bubble is trivial — read it and put it in the element:

The harder part is horizontal placement: the bubble has to slide in step with the thumb. That means working out what percentage of the track the current value represents, and a helper function keeps that math tidy:

range.addEventListener("input", () => {
  bubble.innerHTML = rangel.value;
});

The function has to respect the range's min and max, mapping the value onto a 0–100 scale. Not every range runs 0 to 100, so a value of 50 inside a 0–200 range is 25% of the way along. Accounting for both attributes handles that.

A naive percentage has a visible flaw, though: the bubble sits too far left at the start and too far right at the end. The thumb's edge is inset from the track, scrollbar-style, so its centre at either extreme is never exactly at the boundary. A set of offsets severs as reasonable correction across browsers:

range.addEventListener("input", () => {
  setBubble(range, bubble);
});

function setBubble(range, bubble) {
  const val = range.value;
  const min = range.min ? range.min : 0;
  const max = range.max ? range.max : 100;
  const newVal = Number(((val - min) * 100) / (max - min));
  bubble.innerHTML = val;

  // Sorta magic numbers based on size of the native UI thumb
  bubble.style.left = newVal = "%";
}

Alternative Implementations

Reader Max Globa sent in a version that styles the range input in CSS, which makes the exact thumb size known — some of the arithmetic in his JavaScript still looks like magic, but at least those constants derive from real CSS dimensions.

Several others took the idea elsewhere:

  • Dave Olsen ported the original approach without the jQuery dependency.
  • Sean Stopnik, simurai and Vincent Durand each published their own take.

Worth remembering: range inputs also accept datalists, which render small notches along the track (see range input can have datalists). Ana Tudor has a large collection of range input designs, many of which convey the current value through their visual design.

Appendix: The Original jQuery Version

The earliest version of this technique used jQuery and does not work as well. It is retained for historical reference. The markup:

Styling comes first: the output element is absolutely positioned above the input so that its horizontal position can be adjusted from JavaScript; gradients, border-radius, and a pointer triangle drawn with a pseudo-element dress it up:

output { 
  position: absolute;
  background-image: linear-gradient(top, #444444, #999999);
  width: 40px; 
  height: 30px; 
  text-align: center; 
  color: white; 
  border-radius: 10px; 
  display: inline-block; 
  font: bold 15px/30px Georgia;
  bottom: 175%;
  left: 0;
  margin-left: -1%;
}
output:after { 
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border-top: 10px solid #999999;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  margin-top: -1px;
}

The script then watches every range input for value changes and updates the bubble's left position as the slider moves. Because sliders can be any width and carry any minimum or maximum, the shift is calculated rather than hard-coded. The code, commented:

// DOM Ready
$(function() {
 var el, newPoint, newPlace, offset;
 
 // Select all range inputs, watch for change
 $("input[type='range']").change(function() {
 
   // Cache this for efficiency
   el = $(this);
   
   // Measure width of range input
   width = el.width();
   
   // Figure out placement percentage between left and right of input
   newPoint = (el.val() - el.attr("min")) / (el.attr("max") - el.attr("min"));
   
   // Janky value to get pointer to line up better
   offset = -1.3;
   
   // Prevent bubble from going beyond left or right (unsupported browsers)
   if (newPoint < 0) { newPlace = 0; }
   else if (newPoint > 1) { newPlace = width; }
   else { newPlace = width * newPoint + offset; offset -= newPoint; }
   
   // Move bubble
   el
     .next("output")
     .css({
       left: newPlace,
       marginLeft: offset + "%"
     })
     .text(el.val());
 })
 // Fake a change to position bubble at page load
 .trigger('change');
});

The least satisfying piece is the value 1.3, an attempt to line the tip of the bubble's triangle up with the slider's centre — which, as noted above, never sits at 100% left or right. It is imperfect, but better than leaving it out.

The output styling and styling are usable without adding it to the input; browsers without range support receive the bubble behaviour without it.

A significant catch: this version depends on range inputs carrying explicit min and max attributes. Without them it misbehaves, even though the defaults appear to be 0 and 100. Making it bulletproof means reading each attribute, validating it, and fixing it when it looks wrong:

var minValue, maxValue;
if (!el.attr("min")) { minValue = 0; } else { minValue = el.attr("min"); }

The corrected number then substitutes for the min value (minValue) in the main calculation, with the same treatment applied to max. That firm of update, and the cleaner code, is the improvement that the rewrite addresses.