Given a UL, how to remove all items after 5, if any?

With jQuery is there a way remove an LI items after position 6?

<ul>
 <li>1</li>
 <li>2</li>
 <li>3</li>
 <li>4</li>
 <li>5</li>
 <li>6</li>
 <li>7</li>
</ul>

A way to dynamically remove items 6+ (6 & 7 in this case) and not break if there are less than 6 items? Any ideas? Thanks

Answers


Just:

$('ul li:gt(4)').remove();

http://jsfiddle.net/zerkms/dpCsj/

PS: in your title you say "after 5" and "after 6" is in the question body. I've chosen the one from the title


Can be accomplished with an nth-child selector:

$( 'ul li:nth-child( n + 6 )' ).remove( );


Need Your Help

insert something into kill ring in Emacs

emacs elisp yank kill-ring

I want to write a function that will insert the file name of the current buffer into the kill ring so I can yank it to the terminal in another window. How can I programmatically insert a string int...

How to get user Names of domain in Sharepoint web part?

sharepoint visual-studio-2010

I want to get user names in active directory by using sharepoint webpart.