• reply1
  • reply2
  • reply3
  • reply4
  • reply5
  • reply6
  • ...">

    JQuery advance selector

    <ul id="comment1">
     <li>reply1</li>
     <li>reply2</li>
     <li>reply3</li>
     <li>reply4</li>
     <li>reply5</li>
     <li>reply6</li>
     <li>reply7</li>
     <li>reply8</li>
    </ul>
    

    how to select last 3 or 4 answers using jquery?

    +3
    source share
    2 answers

    You are looking for a slicemethod:

    To get the last four items (updated with -4 as suggested by @KennyTM):

    $('ul#comment1 li').slice(-4).css('color', '#00ff00');
    
    +6
    source
    $('#comment1 li:last-child')
         .prev('li').andSelf()
         .prev('li').andSelf()
         .prev('li').andSelf()
         .prev('li').andSelf()
    

    Should get the last four

    +1
    source

    Source: https://habr.com/ru/post/1756403/


    All Articles