After the 4th <a>
element is found, how would I .hide()
the rest? Below is the code I've written so far:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
if($('a').length >= 4) {
window.alert('4 or more');
}
});
</script>
<a>test </a><br>
<a>fed </a><br>
<a>fdes </a><br>
<a>grr </a><br>
<a>rerf </a><br>
<a>dferf </a>
Any ideas?
Use :gt(index) selector:
$('a:gt(3)').hide();
Or the faster slice
function:
$('a').slice(4).hide();
Live DEMO
Because :gt() is a jQuery extension and not part of the CSS specification, queries using :gt() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").slice(index) instead.
CSS solution
ul li:nth-child(n+5) {
display: none
}
http://jsfiddle.net/mckanet/Sp6yE/
As simple as that:
$("a:gt(3)").hide();
DEMO: http://jsfiddle.net/gmrvK/
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