Search code examples
javascriptjqueryhtmljquery-ui-slider

LocalStorage noUiSlider values || Important! : (


I wanna storage 2 variables(?) values to local storage and load them if the website is refreshed.

var slider = document.getElementById('slider');

noUiSlider.create(slider, {
	start: [60, 600],
	connect: true,
	range: {
		'min': 50,
		'max': 700
	}
});

var sliderValue = slider.noUiSlider.get();
var min = parseInt(sliderValue[0]); //Need to storage this value
var max = parseInt(sliderValue[1]); //Need to storage this value
console.log(Math.floor((Math.random() * (max-min)) + min));
#slider {
  width: 400px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.2.1/nouislider.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.2.1/nouislider.min.js"></script>



<div id="slider">

</div>

!Important! My js knowledge is kinda little so if you wanna explain me anything, talk like you are talking to a baby. Indeed.


Solution

  • Here you go with the solution https://jsfiddle.net/ozfpr1cn/1/

    var slider = document.getElementById('slider');
    
    var start;
    if(localStorage.getItem('noUISlider') == null){
    	start = [60, 600];
    }else{
    	start = $.map(localStorage.getItem('noUISlider').split(","), function(value){
        return parseFloat(value, 10);
      });
    }
    
    noUiSlider.create(slider, {
    	start: start,
    	connect: true,
    	range: {
    		'min': 50,
    		'max': 700
    	}
    });
    
    var sliderValue = slider.noUiSlider.get();
    slider.noUiSlider.on('change', function(){
    	localStorage.setItem('noUISlider', slider.noUiSlider.get());
    });
    #slider {
      width: 400px;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.2.1/nouislider.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.2.1/nouislider.min.js"></script>
    
    <div id="slider">
    
    </div>