Categories: 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…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago