0

I have an input range. Every time I slide the dot along the bar, I got a value number showing right below the dot.

I would like the bar will show 2 different colors when the dot moves: Left side of the dot, the bar is in black indicating a selected value and the right side of the dot will be default color.

Please give a hand. Thanks.

const
    range = document.getElementById('range'),
   devicesRange = document.getElementById('devices-range'),
    setValue = ()=>{
        const
            newValue = Number( (range.value - range.min) * 100 / (range.max - range.min) ),
            newPosition = 10 - (newValue * 0.2);
        devicesRange.innerHTML = `<span>${range.value}</span>`;
        devicesRange.style.left = `calc(${newValue}% + (${newPosition}px))`;
    };
document.addEventListener("DOMContentLoaded", setValue);
range.addEventListener('input', setValue);
.range-wrap{
    /* width: 500px; */
    position: relative;
}
#range {
  width: 100%;
}
.devices-range-value{
    position: absolute;
    top: 30px;
}
.devices-range-value span{
    width: 30px;
    height: 24px;
    line-height: 24px;
    text-align: center;
    background: white;
    color: #000;
    font-size: 12px;
    display: block;
    position: absolute;
    left: 50%;
    transform: translate(-50%, 0);

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="range-wrap">
        <input id="range" type="range" min="1" max="100" value="1" step="1">
        <div class="devices-range-value" id="devices-range"></div>
    </div>
abcid d
  • 2,821
  • 7
  • 31
  • 46
  • 1
    Not sure I understood correctly, but does [this](https://stackoverflow.com/questions/18389224/how-to-style-html5-range-input-to-have-different-color-before-and-after-slider) answer your question? – awarrier99 Jun 05 '20 at 17:47
  • Thanks, awarrier99! Yes, it is what I am looking for. – abcid d Jun 10 '20 at 21:26
  • Great, no problem. I'm going to vote to mark the question as duplicate then (excuse the following automatic comment) – awarrier99 Jun 10 '20 at 22:06

0 Answers0