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 8381457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:42:19+00:00 2026-06-09T16:42:19+00:00

I am working a lot with nodes before they are attached to the main

  • 0

I am working a lot with nodes before they are attached to the main page’s DOM; and I need to perform some work depending on whether or not a given node is contained within the main document or not.

My current method is to walk up the parents, via:

if this.el$.closest("body").length > 0

Is there a more appropriate way to do this? (Preferrably one that doesn’t have to walk all of the node’s ancestors?)

  • 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-09T16:42:21+00:00Added an answer on June 9, 2026 at 4:42 pm

    You have several options which execute at several different speeds.

    var $document = $(document);
    var $element = $("#jq-footer");
    var exists;
    
    // Test if the element is within a body
    exists = $element.closest("body").length;
    
    // Test if the document contains an element
    // wrong syntax, use below instead --> exists = $.contains($document, $element);
    exists = $.contains(document.documentElement, $element[0]);
    
    // Test if the element is within a body
    $($element).parents().is("body");
    
    // Manually loop trough the elements
    exists = elementExists($element[0]);
    
    // Used for manual loop
    function elementExists(element) {
        while (element) {
            if (element == document) {
                return true;
            }
            element = element.parentNode;
        }
        return false;
    }​
    

    See Performance Test

    For this test I copied a huge amount of html to traverse over, I copied the source of one of the jQuery pages into a fiddle, stripping out all the script tags leaving only the body and the html in between.

    Feel free you use document instead of “body” and vice-versa or add more tests but it should give you the general idea.

    Edit

    I updated the performance test to use the correct syntax for contains as the previous one was incorrect and always returned true even if the element did not exist. the blow now returns true if the element exists but if you specify a selector which doesn’t exist it will return false.

    exists = $.contains(document.documentElement, $element[0]);
    

    I also added the suggested alternative mentioned by MrOBrian in the comments of the question which is again slightly faster than contains.

    Nice one MrOBrian.

    Edit

    Here is the jsPerf performance test with all the nice charts.

    Thanks Felix Kling for spotting the issue and helping me fix the jsPerf tests.

    Added more test results from the comments, this one is really good:
    jsPerf performance test: dom-tree-test-exists

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Jstree that holds a lot of nodes, some of them have
I've been working a lot with asp.net web forms and one think that I
I've been working a lot lately with perl, still I dont really know how
I'm pretty new to Android dev and still working out a lot of things.
I'm primarily a Ruby guy, but lately I've been working on a lot of
I learnt a lot about weak_ptr working with share_ptr to break cyclic reference. How
I'm working on a project where a lot of bad code is written. Today
i'm working on a project with a lot of images but after i load
I'm working on a code with a lot of $(.blahblah).size()>0 conditions in it. I
I'm working on implementing a cache for a lot of bitmap tiles I have.

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.