Search code examples
javascriptjqueryjquery-uislidetogglerangeslider

custom range slider with color marker for a range value


I have made this range slider. The slider moves as I want. How can I get the value of where the toggle is in this case?

Such as when the toggle is in 1 range, the div should be "The value selected: 1" and so on.

$("#slider").slider({
  value: 50
})
#green {
  background: green;
  width: 100%;
  /* margin-left: 100px; */
  height: 14px;
}

#blue {
  background: blue;
  width: 100%;
  /* margin-left: 100px; */
  height: 14px;
}

#pink {
  background: pink;
  width: 40%;
  /* margin-left: 100px; */
  height: 14px;
}

#yellow {
  background: yellow;
  width: 100%;
  /* margin-left: 100px; */
  height: 14px;
}

#slider {
  background: red;
  display: flex;
}
<script type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/themes/base/jquery-ui.css">
<div id="slider">
  <div id="green" class="ui-corner-all">1</div>
  <div id="blue" class="ui-corner-all">2</div>
  <div id="yellow" class="ui-corner-all">3</div>
  <div id="pink" class="ui-corner-all">4</div>
</div>

jsFiddle Code


Solution

  • In the API documentation you can find

    $( "#slider" ).on( "slidechange", function( event, ui ) {
        let index = Math.ceil($(this).slider("option", "value")/10000*340)-1;
        let selected = $("div#slider").children().eq(index).text();
    });