Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6590505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:18:30+00:00 2026-05-25T17:18:30+00:00

Solved ( sort of ) Well, I think I solved it ( barring edge

  • 0

Solved (sort of)

Well, I think I solved it (barring edge cases) using the following:

function findByDepth(parent, child, depth){
    var children = $();
    $(child, $(parent)).each(function(){
        if($(this).parentsUntil(parent, child).length == (depth - 1)){
            children = $(children).add($(this));
        }
    });
    return children;
}

// this would successfully return the 3.X elements of an HTML snippet structured
// as my XML example, where <parent> = #parent, etc.
var threeDeep = findByDepth('#parent', '.child', 3);

However somebody has to get the accepted answer here, and I’m not going to answer it myself and abscond with your well earned rep. So, if anyone wants to add anything, such as provide insight into optimizing this function (before I go and $.extend() it in) I’ll likely mark your answer correct, otherwise falling back to marking whoever was first on my initial question.

By the way, check it in the fiddle: http://jsfiddle.net/5PDaA/

Sub-update

Looking again at @CAFxX’s answer, I realized that his approach is probably faster, taking advantage of querySelectorAll in browsers that can. Anyways, I revised his approach to the following, yet it’s still giving me guff:

$.fn.extend({
    'findAtDepth': function(selector, depth){
        var depth = parseInt(depth) || 1;
        var query = selector;
        for(var i = 1; i < depth; i++){
            query += (' ' + selector);
        }
        return $(query, $(this)).not(query + ' ' + selector);
    }
});

It works fine the first time, however as context changes to an element found in the selector, it fails for some reason.


Update

Alright, I was foolishly unclear, and uninformed as to the specs for what I’m doing. Since I’ve reviewed my implementation I’ll update here; I’ll mark the earliest answer that meets my initial requirements given people think me a fool for updating so gratuitously (I wouldn’t blame you) however my bonus mention below is essentially a requirement. I’d post another question, but it’d likely get closed from duplication. Regardless, +1’s all around for your patience:

Depth specification of a given child is necessary (given it’s wrapped in a function or otherwise) thus isolating a child and equally nested (not necessarily siblings) matching elements.

For instance (XML for brevity):

<!-- depth . sibling-index-with-respect-to-depth -->

<parent>
    <child>                     <!-- 1.1 -->
        <child>                 <!-- 2.1 -->
            <child>             <!-- 3.1 -->
                <child></child> <!-- 4.1 -->
            </child>            
            <child>             <!-- 3.2 -->
                <child></child> <!-- 4.2 -->
            </child>            
        </child>                
        <child>                 <!-- 2.2 -->
            <child></child>     <!-- 3.3 -->
        </child>                
    </child>                    
</parent>

Given a depth specified of 2, all 2.X elements are selected. Given 4 all 4.X, and so on.


Original Question

Using the native functionality of jQuery, is there a way to select only the “first-generation” of descendants matching a selector? For instance:

Note: The following is only an example. .child elements are nested in a parent at an arbitrary level.

Bonus: As my proposed syntax below indicates, an answer that provides a way to specify a depth to which the selection should traverse would be incredible.

// HTML
<div id="parent">
    <div>
        <div class="child"> <!-- match -->
            <div>
                <div class="child"> <!-- NO match -->
                </div>
            </div>
        </div>
        <div class="child"> <!-- match -->
            <div>
                <div class="child"> <!-- NO match -->
                </div>
            </div>
        </div>
    </div>
</div>

And:

// jQuery
$('#parent').find('.child:generation(1)'); // something in that vein

Trying to select from the context of #parent, the jQuery :first doesn’t work here as it only hits the first matched .child.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:18:31+00:00Added an answer on May 25, 2026 at 5:18 pm

    Try this (KISS!):

    $("#parent .child").not(".child .child");
    

    edit: to get the second level:

    $("#parent .child .child").not(".child .child .child");
    

    the third:

    $("#parent .child .child .child").not(".child .child .child .child");
    

    and so on… so you could have (untested):

    function findChildrenDeepDown(parentSelector, level) {
      level = parseInt(level);
      var firstSelctor = parent, notSelector = ".child", i;
      for (i=0; i<level; i++) {
        firstSelector += " .child";
        notSelector += " .child";
      }
      return $(firstSelector).not(notSelector);
    }
    
    findChildrenDeepDown("#parent", 3); // find third-level children of #parent
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SOLVED in the last answer im getting following error, dunno where and why? cause
SOLVED. Code has been edited to reflect solution. Given the following GridView : <asp:GridView
Well may be it is to easy question but: I want to sort the
I have sort of a tricky problem I'm attempting to solve. First of all,
SOLVED: Nevermind, the links were visited, and the border definition was missing for visited
Solved I actually found out what is going on here. Turns out it was
solved: IstBestellwert = grouped.Sum(o => (double)o.SollMenge * (double)o.Preis) works My SQL Statment SELECT ABId,
@Solved The two subquestions I have created have been solved (yay for splitting this
SOLVED: This is what was wrong: current.addFolder(folder); (in the final else clause of the
SOLVED I misspelled the property department -> Department. Thanks for the answers and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.