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
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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 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…
Suggested Courses

627 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