Search code examples
javascriptjqueryrangeslider

how to change slider by input fields?


JAVASCRIPT I have this jQuery range slider with js code, I want to change the slider when I put the number in input fields, jsfiddle is also given: https://jsfiddle.net/akhlaque/6Lrtpjwk/1/

var details = {
"books": 20,
"pens": 60,
"ink": 90,
"table": 120,
"chair": 170,
"shoes": 220,
"boots": 320,

};

$(document).ready(function() {

  $(".price_range").slider({
      max: 500,
      range: true,
      values: [0, 500],
      change: function(event, ui) {
          getDetails(ui.values[0], ui.values[1]);
      }
  });

  var current = $(".price_range").slider("option", "values");
  getDetails(current[0], current[1]);

});

function getDetails(minimum, maximum) {

  $("#range").val("" + minimum);
  $("#range-1").val("" + maximum);
  var result = "<table><tr><th>Product Name</th>  <th>Price (in $)</th></tr>";
  for (var item in details) {
      if (details[item] >= minimum && details[item] <= maximum) {
          result += "<tr><td>" + item + "</td><td>" + details[item] + "</td></tr>";
      }
  }
  result += "</table>";
  $(".output").html(result);

}

$(document).ready(function(){

$(".input_slides").change(function(){

var $this = $(this);
$(".price_range").slider("values", $this.data("index"), $this.val());

}); })

enter code here

Solution

  • I made two modifications.

    1. added "index" as an attr to the HTML inputs. To use to target which handle on slider to use.
        <form>
          <input type="text" index="0" id="range" class="input_slides">
          <input type="text" index="1" id="range-1" class="input_slides">
        </form>
    
    
    1. Updated the change value to the new index attr
      $(".input_slides").change(function() {
        var $this = $(this);
        $(".price_range").slider("values", $this.attr('index'), $this.val());
        });
    
    

    This will resolve the direct issue.

    Here is the fiddle: https://jsfiddle.net/eczh8ymk/