Explain Codes LogoExplain Codes Logo

Html slider with two inputs possible?

html
responsive-design
web-development
best-practices
Nikita BarsukovbyNikita Barsukov·Nov 18, 2024
TLDR

To create a functional double-thumb slider, use two <input type="range"> elements. These elements allow for interactive multi-value selection. Here's the fundamental code for a dual-range slider:

<input type="range" id="lower" oninput="upper.value=this.value<this.max?this.value:upper.value" min="0" max="100" step="1"> <input type="range" id="upper" oninput="lower.value=this.value>lower.min?this.value:lower.value" min="0" max="100" step="1">

In this setup, the sliders react to each other, maintaining a valid range while prohibiting value overlaps. This standalone solution is both reactive and minimal, eliminating the need for bulky frameworks.

Improving slider: CSS & JavaScript

By adding a few more lines of code, you can greatly enhance the appearance and usability of your slider, providing the user with an engaging and interactive experience.

Styling with CSS

Apply CSS to your sliders to make them look elegant and professional. The following is a basic example of how you can accomplish that:

// Because styling is the new black... input[type='range'] { -webkit-appearance: none; width: 100%; margin: 5px 0; } input[type='range']:focus { // Invisible invisibility cloak outline: none; } /* Further styling for the thumb and track */

Keep in mind, you can completely tailor the thumb and track styles to match your design aesthetics while ensuring cross-browser compatibility.

Interactive JavaScript

Use the oninput event listener to update the sliders' values. This allows the sliders to react in real time as the user interacts with them.

// When input checks, we update document.getElementById('lower').addEventListener('oninput', function() { // Here we update values and visual cues }); document.getElementById('upper').addEventListener('oninput', function() { // More updates, more power, more slider magic });

JavaScript gives you fine control over the behavior of each thumb, ensuring that the values are within a reasonable range and that no conflicts arise between them.

Exploiting noUiSlider

Here you can use noUiSlider, a mighty library, to take your application beyond simple HTML.

// Popping a slider div right here <div id="slider"></div> // Styles and scripts coming in <link href="nouislider.min.css" rel="stylesheet"> <script src="nouislider.min.js"></script>
// Let's bring the slider to life with some JS magic var slider = document.getElementById('slider'); noUiSlider.create(slider, { start: [20, 80], connect: true, // From being nothing to something range: { 'min': 0, 'max': 100 } });

noUiSlider allows you to easily create custom range values and a visually appealing connected slider experience.

Crafting portable code

When building your HTML slider with dual inputs, aim for scalability and reusability. It increases the component's maintainability and makes implementing multiple sliders in various application parts an absolute breeze.

Setting up reusable HTML blocks

Leaning towards a minimalistic HTML structure, you can bring in reusability:

<!-- Who knew sliders could be this simple? --> <div class="range-slider"> <input type="range" class="lower" /*...*/> <span class="slider-value">0</span> <input type="range" class="upper" /*...*/> <span class="slider-value">100</span> </div>

Reusable code such as this is efficient, allowing you to instantiate multiple sliders without rewriting the block for each instance.

Adapting ranges dynamically

For managing the value range between sliders, you can incorporate a function to dynamically update the range. This immediately communicates any changes to the user and prevents any invalid selections.

/* Preserve your sanity, handle range overlap */ function updateRange(lowerSlider, upperSlider) { // Implement logic to adjust range and display updated values };

Styling with noUiSlider

noUiSlider also offers built-in features to visually show the connection between the handles of a slider. Implement these features for an improved user experience.

/* What connects the sliders, stays together */ .noUi-connect { background: #abc; }

Users can now view the selected range at a glance, making your dual-input slider more intuitive and user-friendly.