Skip to content
the poor coder

price range slider bootstrap 4

Poor Man
Published
Reading time
2 min read
On this page

Price Range Slider Bootstrap 4#

If you are looking for a price range slider in Bootstrap 4, there are different ways to implement it. One of the most common solutions is to use the noUiSlider plugin, which is a lightweight and flexible range slider.

Step 1: Download and Include noUiSlider Files#

You can download the noUiSlider files from their website or from their GitHub repository. Once you have the files, include the CSS and JavaScript files in your HTML code:

HTML
<head>
    <link rel="stylesheet" href="path/to/nouislider.min.css">
    <script src="path/to/nouislider.min.js"></script>
</head>

Step 2: Create HTML Markup#

Create a div element with an id to contain the slider. You can also add labels for the minimum and maximum values:

HTML
<div id="price-slider"></div>
<p>Price Range: <span id="slider-value"></span></p>
<p>Min: <span id="slider-min"></span>, Max: <span id="slider-max"></span></p>

Step 3: Initialize noUiSlider#

In your JavaScript code, initialize the noUiSlider with the desired options. For example, you can set the range of values, the initial values, the step size, the orientation, and the format of the labels:

JavaScript
var slider = document.getElementById('price-slider');
var valueMin = document.getElementById('slider-min');
var valueMax = document.getElementById('slider-max');
var valueSpan = document.getElementById('slider-value');

noUiSlider.create(slider, {
    start: [100, 500],
    connect: true,
    step: 10,
    range: {
        'min': 0,
        'max': 1000
    },
    orientation: 'horizontal',
    format: wNumb({
        decimals: 0,
        thousand: ','
    })
});

slider.noUiSlider.on('update', function (values, handle) {
    var value = values[handle];
    if (handle) {
        valueMax.innerHTML = value;
    } else {
        valueMin.innerHTML = value;
    }
    valueSpan.innerHTML = slider.noUiSlider.get();
});

Step 4: Style the Slider#

You can customize the appearance of the slider by adding CSS rules. For example, you can change the color of the handles and the track, add a border, or change the font size of the labels.

CSS
#price-slider {
    height: 10px;
    margin: 30px 0;
    background-color: #ddd;
}

.noUi-connect {
    background-color: #007bff;
    border-radius: 5px;
}

.noUi-handle {
    width: 20px;
    height: 20px;
    border: 2px solid #007bff;
    background-color: #fff;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}

That's it! You now have a price range slider in your Bootstrap 4 website or web application.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC