Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would make a few changes:</p> <ol> <li><p>Give your list of products an <code>id</code> attribute and give each product a <code>data-price</code> attribute that is equal to the price of the item:</p> <pre><code>&lt;ul id="products"&gt; &lt;li data-price="10"&gt; product - £10 &lt;/li&gt; &lt;li data-price="50"&gt; product - £50 &lt;/li&gt; &lt;li data-price="100"&gt; product - £100 &lt;/li&gt; &lt;li data-price="150"&gt; product - £150 &lt;/li&gt; &lt;li data-price="200"&gt; product - £200 &lt;/li&gt; &lt;/ul&gt; </code></pre></li> <li><p>Add a function that will show or hide those products when a <code>slide</code> event occurs:</p> <pre><code>function showProducts(minPrice, maxPrice) { $("#products li").hide().filter(function() { var price = parseInt($(this).data("price"), 10); return price &gt;= minPrice &amp;&amp; price &lt;= maxPrice; }).show(); } </code></pre></li> <li><p>Call that function from the <code>slide</code> event handler:</p> <pre><code>slide: function(event, ui) { var min = ui.values[0], max = ui.values[1]; $("#amount").val("$" + min + " - $" + max); showProducts(min, max); } </code></pre></li> <li><p>Also call that function right after the slider is initialized so that the correct products are hidden initially:</p> <pre><code>var options = { /* snip */ }, min, max; $("#slider-range").slider(options); min = $("#slider-range").slider("values", 0); max = $("#slider-range").slider("values", 1); $("#amount").val("$" + min + " - $" + max); showProducts(min, max); </code></pre></li> </ol> <p>The entire snippet:</p> <pre><code>function showProducts(minPrice, maxPrice) { $("#products li").hide().filter(function() { var price = parseInt($(this).data("price"), 10); return price &gt;= minPrice &amp;&amp; price &lt;= maxPrice; }).show(); } $(function() { var options = { range: true, min: 0, max: 500, values: [50, 300], slide: function(event, ui) { var min = ui.values[0], max = ui.values[1]; $("#amount").val("$" + min + " - $" + max); showProducts(min, max); } }, min, max; $("#slider-range").slider(options); min = $("#slider-range").slider("values", 0); max = $("#slider-range").slider("values", 1); $("#amount").val("$" + min + " - $" + max); showProducts(min, max); });​ </code></pre> <p><strong>Example:</strong> <a href="http://jsfiddle.net/5aPg7/" rel="noreferrer">http://jsfiddle.net/5aPg7/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload