Changing classes of list items depending on the amount of them and current class name

Changing classes of list items depending on the amount of them and current class name

Hi, I'm trying to create a list that contains items which are filters for a search.

The list shows 10 items (max) on the page load.
if there are more than 10 it changes the class of the elements > 10 so they are hidden, and a 'show more' link is appended.

Now if the user has 'unhidden' the previously hidden items and then chooses one, I want the class for all those previously hidden items to change until the user has deselected that item.


e.g. 13 items in list
user 'shows more'
user selects item number 12
page reloads and now all of the items in the list are shown
user de-selects that item
page reloads and the list is back to showing 10 items and a 'show more' item.

Note: the list order does not change.

Heres the code ive used to achieve this;
  1.       if($('#facilities li').size() > 10)
  2. {
  3. var f = "selected-filter";
  4. if($('#facilities li:gt(9)').attr('Class') == f || $('#facilities li:gt(10)').attr('Class') == f || $('#facilities li:gt(11)').attr('Class') == f || $('#facilities li:gt(12)').attr('Class') == f)
  5. {
  6. // Don't hide
  7. }
  8. else
  9. {
  10. // change the class of the li rows if there are more than 10
  11. $('#facilities li:gt(9)').addClass('moreHidden');
  12. $('#facilities').append('<li id="showMore" class="showMore">Show More &raquo;</li>');
  13. }
  14. }
  15. // show the remaning filter options when 'Show more' is clicked on
  16. $('#showMore').click(function() {
  17. $('#facilities li:gt(9)').fadeIn(800);
  18. $('#showMore').fadeOut(800);
  19. });
as you can see, ive used a very inefficient method to check whether the class name of the elements after the 10th item equals 'selected-filter'.

I've used the li:gt(9) later on to un-hide the elements after the 10th item. I would have thought this same technique would have worked for the .attr('Class') checking, but it only seems to work for the exact element ive specified.
The greater than seems useless in this case.

Is there a better, smaller way to write this? It works for what i need currently, but there are future features im planning that will not be practical checking individual indexes the way i've done it.

Sorry if this is a little hard to understand, I'm not all that good at explaining my problems.