jQuery price slider filter values

Stranger Design picture Stranger Design · Dec 9, 2013 · Viewed 15.7k times · Source

I have created a jQuery price slider but I am not sure how to have the filter area start with actual price ranges? Currently it has '$[object Object] - $[object Object]' when I'd prefer it to have something like '$299 - $1099'. Once you start moving the slider selectors the object references are replaced by the correct prices, but I want the filter area to have the correct ranges from the start.

HTML:

<div id="slider-container"></div>
<p>
<label for="amount">Price range:</label>
<input type="text" id="amount" style="border: 0; color: #f6931f; font-weight: bold;" />
</p>
<div id="computers">
<div class="system" data-price="299">div1 -  299</div>
<div class="system" data-price="599">div2 -  599</div>
<div class="system" data-price="1099">div3 -  1099</div>
</div>
<div id="slider-range"></div>

JavaScript:

  $(function() {
    $('#slider-container').slider({
        range: true,
        min: 299,
        max: 1099,
        values: [299, 1099],
        slide: function(event, ui) {
            $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
            var mi = ui.values[ 0 ];
            var mx = ui.values[ 1 ];
            filterSystem(mi, mx);
        }
    });
  $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
  " - $" + $( "#slider-range" ).slider( "values", 1 ) );
});

function filterSystem(minPrice, maxPrice) {
$("#computers div.system").hide().filter(function() {
    var price = parseInt($(this).data("price"), 10);
    return price >= minPrice && price <= maxPrice;
}).show();
}

I based my structure from the following code: http://jsfiddle.net/bZmJ8/32/

Any help is greatly appreciated.

Answer

adeneo picture adeneo · Dec 9, 2013

The create option would be the proper place to set an initial value

$(function () {
      $('#slider-container').slider({
          range: true,
          min: 299,
          max: 1099,
          values: [299, 1099],
          create: function() {
              $("#amount").val("$299 - $1099");
          },
          slide: function (event, ui) {
              $("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
              var mi = ui.values[0];
              var mx = ui.values[1];
              filterSystem(mi, mx);
          }
      })
});

FIDDLE