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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:12:17+00:00 2026-05-28T15:12:17+00:00

Is there a means of detecting whether an element appears before or after another

  • 0

Is there a means of detecting whether an element appears before or after another element in markup? This is regardless of position in DOM. It could be a child, a sibling, a parent or a parent’s parent. This is a general question, so no markup to share.

To clarify – this is in regards to the element’s position in markup, not its display position. Now that I think about it my question is a bit strange because if you have element X and element Y then you can have these scenarios.

//in regards to y
<x />
<y /> //:after

<y /> //:before
<x />

<x><y /></x> //not really before or after is it?
  • 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-28T15:12:18+00:00Added an answer on May 28, 2026 at 3:12 pm

    Yes, sort of. DOM3 introduced Node.compareDocumentPosition, which allows you to compare the position of two elements. The functionality isn’t very friendly: it involves bitmasks: this is a jQuery plugin that should simplify its use.

    This code is only tested on Firefox 9 and the current version of Chromium. Certainly it won’t work in old versions of IE.

    $.fn.docPosition = function(element) {
        if (element.jquery) element = element[0];
    
        var position = this[0].compareDocumentPosition(element);
    
        if (position & 0x04) return 'after';
        if (position & 0x02) return 'before';
    };
    

    Also, an element that contains another is considered to be before it in the structure.


    OK, a little Googling gives me this blog post by John Resig (the creator of jQuery), which includes compatibility with IE <9. (It’s a little ugly: it uses two non-standard bits of functionality: contains and sourceIndex.) This code should be cross-browser:

    $.fn.docPosition = function (element) {
        function comparePosition(a, b) {
            return a.compareDocumentPosition ? 
              a.compareDocumentPosition(b) : 
              a.contains ? 
                (a != b && a.contains(b) && 16) + 
                  (a != b && b.contains(a) && 8) + 
                  (a.sourceIndex >= 0 && b.sourceIndex >= 0 ?
                    (a.sourceIndex < b.sourceIndex && 4) + 
                      (a.sourceIndex > b.sourceIndex && 2) :
                    1)
                + 0 : 0;
        }
    
        if (element.jquery) element = element[0];
    
        var position = comparePosition(this[0], element);
    
        if (position & 0x04) return 'after';
        if (position & 0x02) return 'before';
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference(means full/limited access) in iphone SDK before and after enrolling in
This program I'm doing is about a social network, which means there are users
Is there a way to make the UIPickerView read-only? This means that the user
I notice that there is getWebApplicationContext in org.springframework.web.servlet.mvc.AbstractController . This means that spring programmers
Is there any means to set the number of reduce tasks once a job
Is there are means whereby if files are altered outside of Xcode, that a
So what started as A problem with buttons, just means there is A problem
Is there a way to perform sequential k-means clustering using scikit-learn? I can't seem
I'm wondering if there is a standard or normal means of interpreting time interval
Is there any possible to connect .NET app with PHP? I means, PHP will

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.