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
360 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now