Search code examples
javascripthtmlcssjquery-uijquery-ui-slider

Displaying value of jQuery <input> range within the thumb


I want the current value of a jQuery <input type='range'> to be displayed within the slider's thumb, something like this:

example
I have seen it is possible but I'm not sure where to start with coding it.


Solution

  • $(function() {
      var handle = $("#custom-handle");
      $("#slider").slider({
        create: function() {
          handle.text($(this).slider("value"));
        },
        slide: function(event, ui) {
          handle.text(ui.value);
        }
      });
    });
    body {
      font-family: Arial, Helvetica, sans-serif;
    }
    
    #custom-handle {
      width: 3em;
      height: 1.6em;
      top: 50%;
      margin-top: -.8em;
      text-align: center;
      line-height: 1.6em;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    
    <div id="slider">
      <div id="custom-handle" class="ui-slider-handle"></div>
    </div>