Jump to content

Show/hide multiple <li>'s


wigglesby

Recommended Posts

Hi

 

I have a script that lists a lot of links within an-ordered list.

As the list will be different lengths for different pages (user profiles) I want to display the first 5 links, then have a button that when clicked, will then show the next 5 (including the first 5 - so a total of 10 links)

 

Is this possible?

 

Link to comment
Share on other sites

I found an example using the following:

 

var numShown = 5; // Initial rows shown & index
var numMore = 5; // Increment
var numRows = $('table').find('tr').length; // Total # rows

$(document).ready(function(){
// Hide rows and add clickable div
$('table')
  .find('tr:gt(' + (numShown - 1) + ')').hide().end()
  .after('<div id="more">Show <span>' + numMore + '</span> More</div>');

$('#more').click(function(){
  numShown = numShown + numMore;
  // no more show more if done
  if ( numShown >= numRows ) $('#more').remove();
  // change rows remaining if less than increment
  if ( numRows - numShown < numMore ) $('#more span').html(numRows - numShown);
  $('table').find('tr:lt('+numShown+')').show();
})

})

 

But when clicking on the div 'Show More' the div disappears and no other results are displayed. They remain as style="display:none" in the HTML.

 

Thanks

Link to comment
Share on other sites

Sorry, I updated my code to include using <li>

 

	var numShown = 5; // Initial rows shown & index
	var numMore = 5; // Increment
	var numRows = jQuery('#test').find('li').length; // Total # rows

	jQuery(document).ready(function(){
	 // Hide rows and add clickable div
	 jQuery('#test')
	  .find('li:gt(' + (numShown - 1) + ')').hide().end()
	  .after('<div id="more">Show <span>' + numMore + '</span> More</div>');

	 jQuery('#more').click(function(){
	  numShown = numShown + numMore;
	  if ( numShown >= numRows ) jQuery('#more').remove();
	  if ( numRows - numShown < numMore ) $('#more span').html(numRows - numShown);
	  jQuery('info').find('tr:lt('+numShown+')').show();
	 })

	})

 

Well in firebug, it says the 6th -> nth <li> are style="display: none" but when i view the HTML source, there is no style attribute.

 

My code when viewing the source is:

<div id="test">
<h2>Articles Tagged by: arms</h2>
<ul id="list">
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test1">test1</a></li>
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test2">test2</a></li>
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test3">test3</a></li>
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test4">test4</a></li>
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test5">test5</a></li>
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test6">test6</a></li>
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test7">test7</a></li>
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test8">test8</a></li>
<li><a href="/work/fitbritz_events_new/web/frontend_dev.php/test/test9">test9</a></li>
</ul>
</div>	

 

 

Link to comment
Share on other sites

consider this code hope its helpful

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE> New Document </TITLE>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

  <script>
    var limit = 5;
    $(document).ready(function() {
        $('#morelink').click(function() {
            var counter = 0;
            $('#list > li').each(function() {
                if ($(this).is(':hidden')) {
                    $(this).show();
                    counter++;
                }
                if (counter == limit) {
                    return false;
                }
            });
        });
    });
  
  </script>
</HEAD>

<BODY>
  <ol id="list">
    <li>Node 1</li>
    <li>Node 2</li>
    <li>Node 3</li>
    <li>Node 4</li>
    <li>Node 5</li>
    <li style="display:none">Node 6</li>
    <li style="display:none">Node 7</li>
    <li style="display:none">Node 8</li>
    <li style="display:none">Node 9</li>
    <li style="display:none">Node 10</li>
    <li style="display:none">Node 11</li>
    <li style="display:none">Node 12</li>
    <li style="display:none">Node 13</li>
    <li style="display:none">Node 14</li>
    <li style="display:none">Node 15</li>
  </ol>
  <a id="morelink" href="javascript:;" />Next 5</a>
</BODY>
</HTML>

Link to comment
Share on other sites

Hi thank for the example.

 

Unfortunately that doesnt work.

 

It just lists out all my <li> tags, rather than hiding the first 5.

 

I am pulling the <li> from a db in PHP, so I'm using a foreach(). So I cannot manually give the 6th -nth <li> attribute of style="display: none".

The example code I found worked well, just when clicking on the 'Show more' link, nothing happened....

 

Any more help would be much appreciated :)

Link to comment
Share on other sites

the following code does work but if you have 50 links it will show them and when the page loads it will hide the links after 5

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE> New Document </TITLE>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

  <script>
    var limit = 5;
    $(document).ready(function() {
        $('#morelink').click(function() {
            var counter = 0;
            $('#list > li').each(function() {
                if ($(this).is(':hidden')) {
                    $(this).show();
                    counter++;
                }
                if (counter == limit) {
                    return false;
                }
            });
        });

           $('#list > li').each(function(i) {
                if (i > 4) {
                    $(this).hide();
                }
           });
    });

  </script>
</HEAD>

<BODY>
  <ol id="list">
    <li>Node 1</li>
    <li>Node 2</li>
    <li>Node 3</li>
    <li>Node 4</li>
    <li>Node 5</li>
    <li>Node 6</li>
    <li>Node 7</li>
    <li>Node 8</li>
    <li>Node 9</li>
    <li>Node 10</li>
    <li>Node 11</li>
    <li>Node 12</li>
    <li>Node 13</li>
    <li>Node 14</li>
    <li>Node 15</li>
  </ol>
  <a id="morelink" href="javascript:;" />Next 5</a>
</BODY>
</HTML>

Link to comment
Share on other sites

I managed to get it to work using the following:

 

jQuery(document).ready(function(){
var numShown = 5; // Initial rows shown & index
var numMore = 5; // Increment
var numRows = jQuery('#list li').size(); // Total # rows
alert(numRows);
 // Hide rows and add clickable div
 jQuery('#list')
  .find('li:gt(' + (numShown - 1) + ')').hide().end()
  .after('<div id="more">Show <span>' + numMore + '</span> More</div>');

 jQuery('#more').click(function(){
  numShown = numShown + numMore;
  // no more show more if done
  if ( numShown >= numRows ) jQuery('#more').remove();
  // change rows remaining if less than increment
  if ( numRows - numShown < numMore ) jQuery('#more span').html(numRows - numShown);
  jQuery('#list').find('li:lt('+numShown+')').show();
 })

})

Link to comment
Share on other sites

I managed to get it to work using the following:

 

jQuery(document).ready(function(){
var numShown = 5; // Initial rows shown & index
var numMore = 5; // Increment
var numRows = jQuery('#list li').size(); // Total # rows
alert(numRows);
 // Hide rows and add clickable div
 jQuery('#list')
  .find('li:gt(' + (numShown - 1) + ')').hide().end()
  .after('<div id="more">Show <span>' + numMore + '</span> More</div>');

 jQuery('#more').click(function(){
  numShown = numShown + numMore;
  // no more show more if done
  if ( numShown >= numRows ) jQuery('#more').remove();
  // change rows remaining if less than increment
  if ( numRows - numShown < numMore ) jQuery('#more span').html(numRows - numShown);
  jQuery('#list').find('li:lt('+numShown+')').show();
 })

})

 

Thats nice :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.