Skip to content Skip to sidebar Skip to footer

Jquery Ui Slider Default Value Issues When Using Custom Step Increments

I'm building a form using the 'snap to increments' JQuery UI Slider: https://jqueryui.com/slider/#steps I'm using the following code so that the slider has a custom min of 2501 but

Solution 1:

A possible solution is to correct your addCommas function:

$(function () {
  $("#slider").slider({
    value: 10000,
    min: 2501,
    max: 50500,
    step: 500,
    range: "min",
    slide: function(event, ui) {
      var x = Number(ui.value);
      if (x > 2501) {
        x = x - (x % 500);
      }
      $("#amount").val('$' + addCommas(x));
    }
  });
  $("#amount").val('$' + addCommas($("#slider").slider("value")));

  functionaddCommas(nStr) {
    var x = Number(nStr);
    if (x > 2501) {
        x = x - (x % 500);
    }
    nStr = x.toString();
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
  }
});
<linkhref="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"rel="stylesheet"/><scriptsrc="https://code.jquery.com/jquery-1.12.1.min.js"></script><scriptsrc="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script><p><labelfor="amount">Amount ($50 increments):</label><inputtype="text"id="amount"readonlystyle="border:0; color:#f6931f; font-weight:bold;"></p><divid="slider"></div>

Post a Comment for "Jquery Ui Slider Default Value Issues When Using Custom Step Increments"