Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery, hide & show list items after nth item

Say I have an unordered list, like so:

<ul>    <li>One</li>    <li>Two</li>    <li>Three</li>    <li>Four</li>    <li>Five</li> </ul> 

How would I, using JQuery, hide the last 2 list items and have a 'show more' link there, so when clicked upon, the last 2 list items would appear?

<ul>    <li>One</li>    <li>Two</li>    <li>Three</li>    <li style="display:none;">Four</li>    <li style="display:none;">Five</li>    <li>Show More</li> </ul> 
like image 680
Keith Donegan Avatar asked Oct 29 '10 17:10

Keith Donegan


People also ask

What does jQuery hide () do?

The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

How do you do show hide in jQuery?

Syntax: $(selector).hide(speed,callback); $(selector).show(speed,callback);

How do I toggle show and hide in jQuery?

jQuery toggle() MethodThe toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.

What is the opposite of hide in jQuery?

ready(function(){ jQuery('body').


1 Answers

Try the following code example:

$('ul li:gt(3)').hide(); $('.show_button').click(function() {     $('ul li:gt(3)').show(); }); 
like image 141
pex Avatar answered Sep 21 '22 00:09

pex