I'm trying to display the value of a slider in hours and minutes separately. I'm quite new to JS and still trying to wrap my head around how it works.
What I have so far is a working slider that has a max value of 1440 (minutes in 24 hours). In JS I'm trying to divide that into hours and minutes and put these values into the specific HTML ID Tags using innerHtml. Unfortunately, the values are not displayed at all, H3 and the specific IDs remain empty.
I also need a way to update the output when the slider is dragged. Could you have a look at it and give me a hint in the right direction?
EDIT: I added an If-Statement to format the minutes when their value is one digit only.
var slider = document.getElementById("myRange");
var hours = Math.floor(slider.value / 60);
var minutes = slider.value % 60;
var minuteOutput = $("#minutes");
var hourOutput = $("#hours");
var formattedMinutes = ("0"+minutes);
hourOutput.innerHTML = hours;
minuteOutput.innerHTML = minutes;
if(minutes < 10){
minuteOutput.innerHTML = formattedMinutes;}
else {minuteOutput.innerHTML = minutes;}
.slider {
-webkit-appearance: none;
appearance: none;
width: 20em;
height: 1em;
background: #2C4461;
outline: none;
margin-top: 2em;
border-radius: 1em;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
border-radius: 50%;
width: 1.5em;
height: 1.5em;
background: #BAC5D2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slidecontainer">
<h3>
<span id="hours"></span>
<span id="minutes"></span>
</h3>
<input type="range" min="1" max="1440" value="300" class="slider" id="myRange">
</div>