Solved

How do I use jQuery to remove children from a div from a certain nth-child forward (conserve the first x children and remove the rest)?

Posted on 2014-09-03
2
371 Views
Last Modified: 2014-09-03
Hi,

I have an autogenerated code that looks as follows:
<div class="tagcloud"><a href="http://mysite.com/tag/analisys/" class="tag-link-10" title="2 argomenti" style="font-size: 11.6pt;">analisys</a>
    <a href="http://mysite.com/tag/city/" class="tag-link-12" title="9 argomenti" style="font-size: 22pt;">city</a>
    <a href="http://mysite.com/tag/conferenza/" class="tag-link-35" title="1 argomento" style="font-size: 8pt;">Conferenza</a>
    <a href="http://mysite.com/tag/egitto/" class="tag-link-48" title="1 argomento" style="font-size: 8pt;">Egitto</a>
    <a href="http://mysite.com/tag/fashion-2/" class="tag-link-13" title="2 argomenti" style="font-size: 11.6pt;">fashion</a>
    <a href="http://mysite.com/tag/finances-2/" class="tag-link-14" title="2 argomenti" style="font-size: 11.6pt;">finances</a>
    <a href="http://mysite.com/tag/galaxy/" class="tag-link-15" title="2 argomenti" style="font-size: 11.6pt;">galaxy</a>
    <a href="http://mysite.com/tag/life/" class="tag-link-16" title="7 argomenti" style="font-size: 20pt;">life</a>
    <a href="http://mysite.com/tag/lights/" class="tag-link-17" title="2 argomenti" style="font-size: 11.6pt;">lights</a>
    <a href="http://mysite.com/tag/maurizio-damiano/" class="tag-link-42" title="1 argomento" style="font-size: 8pt;">Maurizio Damiano</a>
    <a href="http://mysite.com/tag/montagne-sacre/" class="tag-link-44" title="1 argomento" style="font-size: 8pt;">Montagne sacre</a>
    <a href="http://mysite.com/tag/nature-2/" class="tag-link-18" title="1 argomento" style="font-size: 8pt;">nature</a>
    <a href="http://mysite.com/tag/new-york/" class="tag-link-19" title="7 argomenti" style="font-size: 20pt;">new york</a>
    <a href="http://mysite.com/tag/people/" class="tag-link-20" title="9 argomenti" style="font-size: 22pt;">people</a>
    <a href="http://mysite.com/tag/resources/" class="tag-link-21" title="1 argomento" style="font-size: 8pt;">resources</a>
    <a href="http://mysite.com/tag/sinai/" class="tag-link-46" title="1 argomento" style="font-size: 8pt;">Sinai</a>
    <a href="http://mysite.com/tag/sky/" class="tag-link-22" title="1 argomento" style="font-size: 8pt;">sky</a>
    <a href="http://mysite.com/tag/style/" class="tag-link-23" title="2 argomenti" style="font-size: 11.6pt;">style</a>
    <a href="http://mysite.com/tag/verona/" class="tag-link-45" title="1 argomento" style="font-size: 8pt;">Verona</a>
    <a href="http://mysite.com/tag/ziggurat/" class="tag-link-47" title="1 argomento" style="font-size: 8pt;">Ziggurat</a>
</div>

Open in new window


The length of the children inside the div is variable.
What I would like to do is use jQuery and remove all children from number 13 onwards.
The first twelve remain, whichever they are, the rest go using either hide() or remove()

Could anyone please help?

thanks in advance
0
Comment
Question by:badwolfff
  • 2
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40302423
test page : http://jsfiddle.net/6L2ox1kq/1/

var nth = 4-1; // let only four
$(".tagcloud a:gt(" + nth + ")").remove();

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40302432
work too : http://jsfiddle.net/6L2ox1kq/6/

first level descendent
http://api.jquery.com/child-selector/

var nth = 4-1; // let only four
$(".tagcloud>:gt(" + nth + ")").remove();

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now