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

  • Home
  • SEARCH
  • 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 7780973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:04:38+00:00 2026-06-01T19:04:38+00:00

I needed a method to filter out all elements that are parents of other

  • 0

I needed a method to filter out all elements that are parents of other elements in the result set. I tried to write a plugin:

jQuery.fn.distinctDescendants = function() {
    var nodes = [];
    var result = this;

    jQuery(result).each(function() {
        var node = jQuery(this).get(0);
        if(jQuery(node).find(result).length == 0) {
            nodes.push(node);
        }
    });

    return nodes;
};

When i run the following command on this example page:

jQuery('body, textarea').distinctDescendants();

I get the (wrong) result:

[body.contact-page, textarea, textarea]

This is wrong because body is the parent of at least one other element in the result (both textareas). Therefore the expected result would be:

[textarea, textarea]

What is wrong here?

  • 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-06-01T19:04:39+00:00Added an answer on June 1, 2026 at 7:04 pm

    Why aren’t you using jQuery('body > input') instead?

    You can use the following (verbose) code to achieve what you want; it should work as drop-in replacement of your plugin code.

    jQuery.fn.distinctDescendants = function() {
        var nodes = [];
        var parents = [];
    
        // First, copy over all matched elements to nodes.
        jQuery(this).each(function(index, Element) {
            nodes.push(Element);
        });
    
        // Then, for each of these nodes, check if it is parent to some element.
        for (var i=0; i<nodes.length; i++) {
            var node_to_check = nodes[i];
            jQuery(this).each(function(index, Element) {
    
                // Skip self comparisons.
                if (Element == node_to_check) {
                    return;
                }
    
                // Use .tagName to allow .find() to work properly.
                if((jQuery(node_to_check).find(Element.tagName).length > 0)) {
                    if (parents.indexOf(node_to_check) < 0) {
                        parents.push(node_to_check);
                    }
                }
            });
        }
    
        // Finally, construct the result.
        var result = [];
        for (var i=0; i<nodes.length; i++) {
            var node_to_check = nodes[i];
            if (parents.indexOf(node_to_check) < 0) {
                result.push(node_to_check);
            }
        }
    
        return result;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is the main method needed to write a java program? This is my code:
I needed a method that gets a link to a page and returns the
3 points are needed to define a plane. Newell's method is known to fail
In unit testing, the setup method is used to create the objects needed for
i have implemented keylistener interface and implemented all the needed methods but when i
Was: Design by contract: predict methods needed, discipline yourself and deal with code that
I needed a function that simply checks if a string can be converted to
I'm using NHibernate 3.2 and I have a repository method that looks like: public
So I am figuring out how to set up some options for a class.
How can I propagate all events on one JQuery.Deferred() object to another ? Is

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.