JavaScript Notes
Learn to build a dual-handle price range slider in JavaScript with min/max thumb dragging, range highlighting, live price display, and product filtering.
What You'll Learn
- How to build a dual-handle range slider from scratch
- How to calculate positions using
getBoundingClientRect() - How to keep
minandmaxthumbs from crossing each other - How to highlight the selected range between the two handles
- How to use the slider to filter a product list in real time
Project Overview
A price range slider has two draggable handles. The left handle sets the minimum price, the right handle sets the maximum. As you drag, the colored track between them updates, and the displayed products filter accordingly.
Component Structure
How It Works — Flow Diagram
Step-by-Step Build
Step 1 — HTML Structure
<div class="price-range-section">
<h2>Filter by Price</h2>
<div class="price-display">
Price: $<span id="minDisplay">0</span> — $<span id="maxDisplay">1000</span>
</div>
<div class="slider-container" id="sliderContainer">
<div class="slider-track">
<div class="slider-range" id="sliderRange"></div>
</div>
<div class="thumb thumb-min" id="thumbMin"></div>
<div class="thumb thumb-max" id="thumbMax"></div>
</div>
<div class="price-labels">
<span>$0</span>
<span>$1000</span>
</div>
<h3>Products</h3>
<ul id="productList" class="product-list"></ul>
<p id="productCount" class="count"></p>
</div>Step 2 — State & Config
Step 3 — Convert Between Value and Percentage
function valToPct(val) {
return ((val - CONFIG.min) / (CONFIG.max - CONFIG.min)) * 100;
}
function pctToVal(pct) {
return Math.round(CONFIG.min + (pct / 100) * (CONFIG.max - CONFIG.min));
}Step 4 — Update UI from State
function updateUI() {
const minPct = valToPct(state.minVal);
const maxPct = valToPct(state.maxVal);
document.getElementById("thumbMin").style.left = `${minPct}%`;
document.getElementById("thumbMax").style.left = `${maxPct}%`;
document.getElementById("sliderRange").style.left = `${minPct}%`;
document.getElementById("sliderRange").style.width = `${maxPct - minPct}%`;
document.getElementById("minDisplay").textContent = state.minVal;
document.getElementById("maxDisplay").textContent = state.maxVal;
filterProducts();
}Step 5 — Drag Logic
Step 6 — Touch Support
Step 7 — Filter Products
Step 8 — Initialize
updateUI();
addTouchSupport(document.getElementById("thumbMin"), true);
addTouchSupport(document.getElementById("thumbMax"), false);Console Output
// Initial state: min=0, max=1000 updateUI() → thumbMin at 0%, thumbMax at 100%, range fills 100% filterProducts() → all 7 products in range // User drags thumbMin to $150 state.minVal = 150 updateUI() → thumbMin at 15%, sliderRange.left="15%" filterProducts() → USB-C Hub ($45) out of range → 6 products shown // User drags thumbMax to $500 state.maxVal = 500 updateUI() → thumbMax at 50%, sliderRange.width="35%" filterProducts() → Laptop ($899) out of range → 5 products shown
Edge Cases to Handle
| Situation | Expected Behavior |
|---|---|
| Min would exceed max | Clamp: min ≤ max − 10 |
| Max would go below min | Clamp: max ≥ min + 10 |
| Drag outside container | Clamp pct to 0–100 |
| Mobile touch | Touch events handled separately |
| Zero products in range | Show "0 products found" |
Challenge Yourself
- Input boxes — add number input fields that sync with the slider handles
- Animated products — fade out products that leave the price range
- URL persistence — save min/max in
?min=150&max=500URL params - Step snapping — snap slider to nearest $50 increment
- Histogram overlay — show a bar chart of product distribution above the track
Best Practices
- Use
getBoundingClientRect()to measure the track dynamically — don't hardcode width - Apply
e.preventDefault()on mousedown to prevent text selection while dragging - Always clamp values and enforce a minimum gap between min and max
- Add both mouse and touch event support for full device compatibility
- Use percentage-based positioning so the slider is naturally responsive
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Build a JavaScript Price Range Slider - Step by Step Project Tutorial.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, mini, projects, price
Related JavaScript Master Course Topics