Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery toggle to work in multiple instances

I have a show/hide effect that works but I just need to be able to use it multiple times in a page. Currently it toggles all elements. Can't get my head around how to do it.

Hope you can help.

http://pastebin.me/29328e556caf53e9a1925030d65b864b

like image 344
mark Avatar asked Feb 01 '26 04:02

mark


1 Answers

You can edit your function a bit to this, it'll work on each <ul> indepdently:

$('.facet ul').each(function() {
  if($(this).children('li:gt(9)').hide().length === 0) return;
  $(this).append(
    $('<li id="toggler">More</li>').toggle(function() {
       $(this).text('Hide').siblings().show();
    }, function() { 
       $(this).text('More').siblings('li:gt(9)').hide();
    }));
});​

See a working demo here

Before, it was getting any li over 9, you want to do this per <ul> element with .each() like the example above.

like image 159
Nick Craver Avatar answered Feb 04 '26 01:02

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!