Designer learning to code here, not too far along in my js or using jquery. Attempting to get a checkbox filter pretty much the same as this guy: http://jsfiddle.net/BCbeU/ but can’t seem to get it to run like the jsfiddle on the site I’m building.
Here is my html for the checkboxes:
<div class="tags">
<label><input type="checkbox" rel="all"/> Show All </label>
<label><input type="checkbox" rel="one"/> 1 Bedroom </label>
<label><input type="checkbox" rel="two"/> 2 Bedroom </label>
<label><input type="checkbox" rel="below"/> Below $1500 </label>
<label><input type="checkbox" rel="above"/> Above $1500 </label>
</div>
Here is my html for the items I’m trying to filter:
<div id="shelf1-housing">
<ul class="results">
<li class="all one">
<a href="html/sessionone.html">
<div class="box">
<img src="../images/apartment2.jpg">
<h4>3 Bedroom Apartment</h4>
<h5>$1500 a month</h5>
</div>
</a>
</li>
<li class="all below">
<a href="html/sessionone.html">
<div class="box">
<img src="../images/apartment1.jpg">
<h4>3 Bedroom Apartment</h4>
<h5>$1500 a month</h5>
</div>
</a>
</li>
<li class="below all">
<a href="html/sessionone.html">
<div class="box">
<img src="../images/apartment2.jpg">
<h4>3 Bedroom Apartment</h4>
<h5>$1500 a month</h5>
</div>
</a>
</li>
<li class="all above">
<a href="html/sessionone.html">
<div class="box">
<img src="../images/apartment1.jpg">
<h4>3 Bedroom Apartment</h4>
<h5>$1500 a month</h5>
</div>
</a>
</li>
<li class="all">
<a href="html/sessionone.html">
<div class="box">
<img src="../images/apartment2.jpg">
<h4>3 Bedroom Apartment</h4>
<h5>$1500 a month</h5>
</div>
</a>
</li>
</ul><!--End of Results-->
</div><!--End of Shelf1-Housing-->
And here is the javascript:
$('div.tags').delegate('input:checkbox', 'change', function(){
var $lis = $('.results > li').hide();
//For each one checked
$('input:checked').each(function(){
$lis.filter('.' + $(this).attr('rel')).show();
});
});
Thanks in advance to for the help. Still learning the basics, and they haven’t got me this far yet, soon, but not yet 🙂
I have created fiddle by copying code from your website. Please check http://jsfiddle.net/LXymq/
Following changes made:
1) Added the Li back to a div with class called results. This is how you had it.
<div class="results"> ... All LIs ... </div>2) Added all your code to $(document).ready function as the code was getting executed before the dom was ready.
$(document).ready(function() { ... all your code inside this .... });4) Hardcoded the image path for beauty.