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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handling case and special names in javascript 3 48
Add Rows on a Table 8 49
close window in firefox 3 37
How do I refresh ajax data from controller using angular? 3 19
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

737 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