You are designing a search bar for a fashion e-commerce website. The client wants users to be able to filter
products by price range. Which HTML input type(s) would you use to create a price range slider, and how
would you implement it?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To create a price range slider for a fashion e-commerce website, you can use the <input type="range"> HTML element. However, since a single range input only allows for one value, you'll need two range inputs to represent the minimum and maximum price in the range. Here's how you can implement it:
const minPriceInput = document.getElementById('min-price');
const maxPriceInput = document.getElementById('max-price');
const minPriceValue = document.getElementById('min-price-value');
const maxPriceValue = document.getElementById('max-price-value');
// Display initial values
minPriceValue.textContent = minPriceInput.value;
maxPriceValue.textContent = maxPriceInput.value;
// Update min price value on change
minPriceInput.addEventListener('input', function() {
minPriceValue.textContent = minPriceInput.value;
if (parseInt(minPriceInput.value) > parseInt(maxPriceInput.value)) {
minPriceInput.value = maxPriceInput.value;
minPriceValue.textContent = maxPriceInput.value;
}
});
// Update max price value on change
maxPriceInput.addEventListener('input', function() {
maxPriceValue.textContent = maxPriceInput.value;
if (parseInt(maxPriceInput.value) < parseInt(minPriceInput.value)) {
maxPriceInput.value = minPriceInput.value;
maxPriceValue.textContent = minPriceInput.value;
}
});
Explanation:
HTML:
Two <input type="range"> elements are used for the minimum and maximum price.
The min and max attributes define the price range (e.g., 0 to 1000).
<span> elements display the current values of the sliders.
JavaScript:
Ensures the minimum price slider cannot exceed the maximum price slider and vice versa.
Updates the displayed values in real-time as the user adjusts the sliders.
This implementation provides a user-friendly price range filter for the e-commerce website. You can further enhance it by adding a "Filter" button or dynamically updating the product list based on the selected range.