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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:52:25+00:00 2026-05-17T14:52:25+00:00

Given two jquery objects, Is there some way I tell which one is further

  • 0

Given two jquery objects, Is there some way I tell which one is “further ahead” in the document tree than the other? In other words, with a document

 <p id="p1" ></p>
 <div id="div1">
    <p id="p2"></p>
 </div>
 <p id="p3"></p>

Is there some function that behaves thus?

$("#p1").isBefore($("#p2")); // == true
$("#p3").isBefore($("#p2")); // == false
$("#p1").isBefore(#("#p3")); // == true

Note that I care about position in the HTML tree of the document, not physical position on the screen.

  • 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-17T14:52:26+00:00Added an answer on May 17, 2026 at 2:52 pm

    You can make a function that does this, like this:

    (function($) {
      $.fn.isBefore = function(elem) {
        if(typeof(elem) == "string") elem = $(elem);
        return this.add(elem).index(elem) > 0;
      }
    })(jQuery)
    

    You can try it out here, the first line is so it can also take a selector string directly, for example:

    $("#p1").isBefore("#p2");
    

    What this does is .add() the additional element (or selector) (which jQuery keeps in document order) and then checks if it’s the second of the two.

    If the selector this is run against has more than one element, this returns true if any of those elements are “before” the passed in element or selector, so given your markup for example $("p").isBefore("#p2") would be true, since at least one <p> occurs “before” #p2.

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

Sidebar

Related Questions

I'm working on a Javascript/jQuery powered image preloader, and have hit a bit of
I have a number of Jquery plugins being called through my Drupal Acquia Marina
I'm working with some code that adds and removes various CSS classes on the
I'm making a jquery clone for C#. Right now I've got it set up
basically I have a for loop that give me two values, id, and value
I know that given enough time I'll figure this out, but it sure would
I'm implementing a small voting system on my website. I came up with three
Hi I have a javascript array object rapresenting the amount of items sold in
I'm trying to construct a table with dynamically build information. My thought was to

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.