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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:22:18+00:00 2026-05-23T15:22:18+00:00

I need selectors which are able to retrieve correct results for visual closeness queries

  • 0

I need selectors which are able to retrieve correct results for visual closeness queries like the following:

  • $("h2:closestTo('div#example')");
  • $("input:below('label:contains(\"Username\")')");

Closeness and distances should be dependent of the visual location of elements rendered in the web browser (and differs for different window sizes).

Is there any way to do that with jQuery? I think it will involve the usage of .offset()

  • 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-23T15:22:18+00:00Added an answer on May 23, 2026 at 3:22 pm

    I just created a first working version by myself.

    Demonstration: http://jsfiddle.net/alp82/L5ZM8/

    Definition of “above”: Only elements which are visually above a reference element are returned. Also, they have to be in horizontal bounds of the reference elements. The same rules apply to “below”, “leftTo” and “rightTo”.

    Here is the code:

    jQuery.expr[':'].above = function(node, stackIndex, properties) {
        return inDirection(node, "above", properties[3]);
    };
    
    jQuery.expr[':'].below = function(node, stackIndex, properties) {
        return inDirection(node, "below", properties[3]);
    };
    
    jQuery.expr[':'].leftTo = function(node, stackIndex, properties) {
        return inDirection(node, "leftTo", properties[3]);
    };
    
    jQuery.expr[':'].rightTo = function(node, stackIndex, properties) {
        return inDirection(node, "rightTo", properties[3]);
    };
    
    inDirection = function(node, direction, selector) {
        var topSource = jQuery(selector).offset().top;
        var leftSource = jQuery(selector).offset().left;
        var bottomSource = topSource + jQuery(selector).height();
        var rightSource = leftSource + jQuery(selector).width();
    
        var topTarget = jQuery(node).offset().top;
        var leftTarget = jQuery(node).offset().left;
        var bottomTarget = topTarget + jQuery(node).height();
        var rightTarget = leftTarget + jQuery(node).width();
    
        if(direction == "above") {
            return bottomSource > bottomTarget && leftSource <= rightTarget && rightSource >= leftTarget;
        }
    
        if(direction == "below") {
            return topSource < topTarget && leftSource <= rightTarget && rightSource >= leftTarget;
        }
    
        if(direction == "leftTo") {
            return rightSource > rightTarget && topSource <= bottomTarget && bottomSource >= topTarget;
        }
    
        if(direction == "rightTo") {
            return leftSource < leftTarget && topSource <= bottomTarget && bottomSource >= topTarget;
        }
    
        return false;
    };
    

    Use the selector like in the following examples. The argument to the custom selector has to be a selector which returns exactly one element:

    $(document).ready(function() {
        elementsAbove = jQuery("div:above('#footer')");
        elementsBelow = jQuery("div:below('#header')");
        elementsLeftTo = jQuery("div:leftTo('#sidebar-right')");
        elementsRightTo = jQuery("div:rightTo('#sidebar-left')");
    }
    

    Please tell me if that works for you and/or you have suggestions to make it better.

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

Sidebar

Related Questions

I need a CSS selector that can find the 2nd div of 2 that
Need to locate the following pattern: The letter I followed by a space then
I'm a little confused about which jQuery method and/or selectors to use when trying
Ideally I would like to be able to make the event target all p.hiddeninput,
I have a set of tables which basically look like this AGENCY - AGENCY_NAME
I need to be able to have AVCaptureVideoDataOutput and AVCaptureMovieFileOutput working at the same
I have a scenario in which I want user to be able to start
i have a scrollview which has images added as subviews. i need to be
I have a sorting comparator which I need to use in a few different
I have a CCLayer which I need to add a camera to, on top

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.