0

I'm attempting to create a filterable photo gallery using jQuery and multiple classes. I have some code set up, but it doesn't seem to work. Can anybody give me any insight on how to fix this function?

    $(document).ready(function(){
      $('#sorter a').click(function(e){
        var sortName = $(this).text().toLowerCase().replace(' ','-');
        if(sortName === 'all-images'){
        $('#photorow1 li').show().removeClass('hidden');
          } 
        else {
   $('#photorow1 li').filter(sortName).show().removeClass('hidden')
               .end().not(sortName).hide().addClass('hidden');
         }
e.preventDefault();
 });
});

Any help would be greatly appreciated!!

*updated code

Christian Benincasa
  • 1,215
  • 1
  • 21
  • 45

2 Answers2

1

The problem is you're doing a return false before any work is being done, move that to the end of your click handler :)

Overall you can clean it up a bit, something like this should do:

$(function(){
  $('#sorter a').click(function(e){
    var sortName = $(this).text().toLowerCase().replace(' ','-');
    if(sortName === 'all-images') {
       $('#photorow1 li').show();
    } else {
       $('#photorow1 li').filter(filterVal).show().removeClass('hidden')
                   .end().not(filterVal).hide().addClass('hidden');
    }
    e.preventDefault();
  });
});

I recommend that you just add display: none; to the .hidden CSS rules (if you need that class for something else), otherwise just .hide()/.show() works.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • This is working, but it is hiding all the elements and somehow overlooking the elements with the class name that I tried to specify using sortName...if that makes any sense. – Christian Benincasa Aug 07 '10 at 01:51
0

For starters, return false; should be at the end of the function, because any code that comes after it in that function will be ignored.

Plus, you don't need that and e.preventDefault(); in the same function, they overlap a bit. You can read more about their similarities here. Pick one.

Community
  • 1
  • 1
cnanney
  • 2,101
  • 1
  • 13
  • 8