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/
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With