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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:31:48+00:00 2026-05-19T04:31:48+00:00

Does anybody know how I can get the equivalent of jQuery’s .offset() and .closest()

  • 0

Does anybody know how I can get the equivalent of jQuery’s .offset() and .closest() without a JavaScript library?

For .closest(), if I know how far to climb up the DOM tree, I can just use that many .parentNodes, but if I don’t know how far to go, I’m stuck.

  • 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-19T04:31:49+00:00Added an answer on May 19, 2026 at 4:31 am

    jQuery is javascript at its core. You can use this jQuery source viewer to find out implementation of any method.

    For example, here is how closest is implemented:

    closest: function( selectors, context ) {
        var ret = [], i, l, cur = this[0];
    
        if ( jQuery.isArray( selectors ) ) {
            var match, selector,
                matches = {},
                level = 1;
    
            if ( cur && selectors.length ) {
                for ( i = 0, l = selectors.length; i < l; i++ ) {
                    selector = selectors[i];
    
                    if ( !matches[selector] ) {
                        matches[selector] = jQuery.expr.match.POS.test( selector ) ? 
                            jQuery( selector, context || this.context ) :
                            selector;
                    }
                }
    
                while ( cur && cur.ownerDocument && cur !== context ) {
                    for ( selector in matches ) {
                        match = matches[selector];
    
                        if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
                            ret.push({ selector: selector, elem: cur, level: level });
                        }
                    }
    
                    cur = cur.parentNode;
                    level++;
                }
            }
    
            return ret;
        }
    
        var pos = POS.test( selectors ) ? 
            jQuery( selectors, context || this.context ) : null;
    
        for ( i = 0, l = this.length; i < l; i++ ) {
            cur = this[i];
    
            while ( cur ) {
                if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
                    ret.push( cur );
                    break;
    
                } else {
                    cur = cur.parentNode;
                    if ( !cur || !cur.ownerDocument || cur === context ) {
                        break;
                    }
                }
            }
        }
    
        ret = ret.length > 1 ? jQuery.unique(ret) : ret;
    
        return this.pushStack( ret, "closest", selectors );
    }
    

    And here is the implementation of the offset:

    jQuery.fn.offset = function( options ) {
        var elem = this[0], box;
    
        if ( options ) { 
            return this.each(function( i ) {
                jQuery.offset.setOffset( this, options, i );
            });
        }
    
        if ( !elem || !elem.ownerDocument ) {
            return null;
        }
    
        if ( elem === elem.ownerDocument.body ) {
            return jQuery.offset.bodyOffset( elem );
        }
    
        try {
            box = elem.getBoundingClientRect();
        } catch(e) {}
    
        var doc = elem.ownerDocument,
            docElem = doc.documentElement;
    
        // Make sure we're not dealing with a disconnected DOM node
        if ( !box || !jQuery.contains( docElem, elem ) ) {
            return box || { top: 0, left: 0 };
        }
    
        var body = doc.body,
            win = getWindow(doc),
            clientTop  = docElem.clientTop  || body.clientTop  || 0,
            clientLeft = docElem.clientLeft || body.clientLeft || 0,
            scrollTop  = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop ),
            scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
            top  = box.top  + scrollTop  - clientTop,
            left = box.left + scrollLeft - clientLeft;
    
        return { top: top, left: left };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know where I can get the vsdoc for jQuery 1.4.3? Alternatively, what
Does anybody know how I can get the number of the elements (rows*cols) returned
Does anybody know where I can get a free web crawler that actually works
Does anybody know How I can get the ckEdtior to work with Selenium. I'm
Does anybody know, is it possible and how can I get name of user
Does anybody know how i can get multiple popups sent via notify-send to appear
Does anybody know how I can get this xml into the domDocument? Right now,
Does anybody know how you can get the endcap width or height values for
Does anybody know how I can get built-in Django tag url to work in
Does anybody know, if i have an object like $('#input') how can I get

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.