Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide all li elements and show the first 2 and toggle them by button

Tags:

jquery

Assume I have

<ul>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
</ul>

I want jQuery code to hide all <li> then show the fist and the second one then append() an additional <li>more</li> that is used to toggle the hidden li.

like image 402
Mostafa Elkady Avatar asked Feb 19 '11 13:02

Mostafa Elkady


People also ask

How do you hide and show on click?

To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.

How do I show a div when clicking a button?

To display or hide a <div> by a <button> click, you can add the onclick event listener to the <button> element. The onclick listener for the button will have a function that will change the display attribute of the <div> from the default value (which is block ) to none .

How do I hide a button in HTML?

You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <button> is not visible, but maintains its position on the page.


2 Answers

this should do it ..

// hide all li and then show the first two
$('ul li').hide().filter(':lt(2)').show(); 

// append a li with text more that on click will show the rest
$('ul').append('<li>more</li>').find('li:last').click(function(){
    $(this).siblings(':gt(1)').toggle();
});

demo :

$('ul li').hide().filter(':lt(2)').show();
$('ul').append('<li>more</li>').find('li:last').click(function() {
  $(this).siblings(':gt(1)').toggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

Update to add changing text from more to less ..

$('ul li')
    .hide()
    .filter(':lt(2)')
    .show();

$('ul')
    .append('<li><span>more</span><span class="less">less</span></li>')
    .find('li:last')
    .click(function(){
        $(this)
            .siblings(':gt(1)')
            .toggle()
            .end()
            .find('span')
            .toggle();
    });

needs a css rule of .less{display:none}

demo :

$('ul li')
  .hide()
  .filter(':lt(2)')
  .show();

$('ul')
  .append('<li><span>more</span><span class="less">less</span></li>')
  .find('li:last')
  .click(function() {
    $(this)
      .siblings(':gt(1)')
      .toggle()
      .end()
      .find('span')
      .toggle();
  });
.less {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>
like image 177
Gabriele Petrioli Avatar answered Sep 30 '22 19:09

Gabriele Petrioli


Is this something like you're looking for?

 $('li').hide()
        .slice(0,1)
        .addClass('fixed')
        .show();
 $('<li class="toggle">more</li>')
        .appendTo('ul')
        .click( function() {
             $('li:not(.toggle,.fixed)').toggle();
         });
like image 43
tvanfosson Avatar answered Sep 30 '22 21:09

tvanfosson