Development

noUiSlider multi range slider example

A noUiSlider multi-range HTML slider complete with the slider values outputted into the HTML.

noUiSlider is a minimal Javascript range slider framework with a convenient API.

CodePen example.

You will need the noUiSlider Javascript and CSS files which can be found on cdnjs here.

For this multi-range slider the min value will be 0 whilst the max is 100, the step size is 1.

Running an event on when the slider is interacted with uses the event callbacks. The ‘update’ event fits nicely. This event callback is needed to update the text below the slider with the current values.

Getting the values of the slider is done with slider.noUiSlider.get() which returns an array with the value of each slider toggle.

let slider = document.getElementById("slider");

noUiSlider.create(slider, {
  start: [20, 80],
  connect: true,
  step: 1,
  range: {
    min: 0,
    max: 100
  }
});

let min_element = document.getElementById("min");
let max_element = document.getElementById("max");
let dif_element = document.getElementById("dif");

slider.noUiSlider.on("update", function (values, handle) {
  let slider_values = slider.noUiSlider.get();
  min_element.innerHTML = parseInt(slider_values[0]);
  max_element.innerHTML = parseInt(slider_values[1]);
  dif_element.innerHTML = parseInt(slider_values[1] - slider_values[0]);
});

Here is the HTML which is utilizing the Bootstrap framework:

<div class="container">
  <div class="card">
    <div class="card-header text-center">
      <h1>noUiSlider</h1>
    </div>
    <div class="card-body">
      <div class="row text-center m-2">
        <div id="slider"></div>
      </div>
      <div class="row text-center mt-2">
        <div class="col-4">
          <p>Min: <b><span id="min"></span></b></p>
        </div>
        <div class="col-4">
          <p>Dif: <b><span id="dif"></span></b></p>
        </div>
        <div class="col-4">
          <p>Max: <b><span id="max"></span></b></p>
        </div>
      </div>
    </div>
  </div>
</div>

The slider CSS styling:

.noUi-connect {
  background: #a690f4;
}

.noUi-target {
  background: transparent;
  border-radius: 0;
  border: none;
  box-shadow: none;
}

.noUi-connects {
  background: #fab8c4;
}

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago