Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery limit the results/selection returned

Tags:

jquery

Is it possible to limited the results returned in jQuery

<body>
  <div id="box">
    <ul>
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        <li>Four</li>
        <li>Five</li>
        <li>Six</li>        
    </ul>         
  </div>  

  <script>
      $(function(){
          var num = 4;
          console.log( $('#box').children(':first').children(num).text() );
      })
  </script>
</body>

This will output:

OneTwoThreeFourFiveSix

But I want:

OneTwoThreeFour

Demo Fiddle: http://jsfiddle.net/JLqrc/

like image 432
ttmt Avatar asked Dec 28 '25 15:12

ttmt


1 Answers

You can use :lt()

$('#box').children(':first').children(':lt('+num+')');

or as MackieeE commented to shorten the code

$('#box li:lt('+ num +')').text()

DEMO

Documentation : http://api.jquery.com/lt-selector/

like image 161
Anton Avatar answered Dec 30 '25 23:12

Anton



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!