views:

259

answers:

3

Hi,

I'm a jquery newbie - wanted to ask what might be the best strategy for achieving what I am after as I think it uses a mix of jquery functions :

  1. If list of items exceeds 5 items a 'more' link appears which when toggled will reveal the rest of the items. If items list is 5 or less no 'more' link is shown. Hide button also at bottom of long full revealed list. (perhaps this is toggle+pagination?)

  2. And then also that this can be used in multiple instances, as it is for multiple category menu's.(to be used in typical indexhibit websites structure like http://mikeyburton.com/)

  • link 1
  • link 2
  • link 3
  • link 4
  • link 5
  • link 6
  • link 7
  • link 8
  • more

Any help or links greatly appreciated.

+1  A: 

This will give you the number of matched elements in a certain class:

$('.element').size();

You can use hide() and show() as mentioned above, or use toggle() to hide and show the extraneous elements. Or for large lists it might be prudent to load() or get() (ajax) those only when needed.

It looks like you want what's called an "accordian" function. You can also achieve that effect using jQuery UI's accordian widget.

If you want to not show the "more" control then hide it at the appropriate times:

if ("li.all").size() > 5) {
    $("#toggler").hide();
}
jjclarkson
is there an if size > 5 show "more" command?<or if =5 or less hide 'more' ?Sorry I'm bit of a jquery newbie.
mark
A: 

This can be done many ways.

The simplest way would be to load all the items and then hide all but the first 5. This can be done via jQuery's show() and hide() methods. link You can also use the size() method to find out how many items of the same class you have.

Or you can use Ajax to load the other items when needed. link

CeejeeB
+1  A: 

see also SO post to find more answers

mcgrailm
Thanks for the help. I think i've got the show and hide but not sure how to incorporate the ability to hide the 'more' link if it's less than or 5 and how to get it to work in multiple instances on a page - http://www.brianfitzer.ie/test/test10.html
mark
can you show what happens when less than 5 ?
mcgrailm
if there's less it doesn't show the 'more' which seems to work -brianfitzer.ie/test/test10_less.html
mark
ok so now your only problem is multiple instances ?
mcgrailm
Yes. js wasn't rendering right there but fixed that -http://www.brianfitzer.ie/test/test10.js
mark
cool glad you got it working
mcgrailm
any ideas as regards multiple instances?
mark
I think you might have to do any each on the class
mcgrailm