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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
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…

785 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