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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:12:04+00:00 2026-05-16T21:12:04+00:00

On user click I would like to get a list of all elements that

  • 0

On user click I would like to get a list of all elements that resides at the clicked point.

For example, if user clicks on Hello here:

<div><span>Hello<span></div>

I would like to get the following list:

  • <span> element
  • <div> element
  • <body> element
  • <html> element

What would be the easiest method to get these elements ?

  • 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-16T21:12:05+00:00Added an answer on May 16, 2026 at 9:12 pm

    EDIT: Based on clarification, I think this is what you mean:

    EDIT: As pointed out by @Misha, outerWidth() and outerHeight() should be used in lieu of width() and height() in order to get an accurate range.

    Also, if there’s nothing to prevent event bubbling on the page, then the click should be placed on the document as it will be much more efficient. Even if some other click handler prevents bubbling, you should still have the click on the document, and just handle it separately from those handler that prevent bubbling.

    Example: http://jsfiddle.net/57bVR/3/

    $(document).click(function(e) {
        var clickX = e.pageX
            ,clickY = e.pageY
            ,list
            ,$list
            ,offset
            ,range
            ,$body = $('body').parents().andSelf();
    
        $list = $('body *').filter(function() {
            offset = $(this).offset();
            range = {
                x: [ offset.left,
                    offset.left + $(this).outerWidth() ],
                y: [ offset.top,
                    offset.top + $(this).outerHeight() ]
            };
            return (clickX >= range.x[0] && clickX <= range.x[1]) && (clickY >= range.y[0] && clickY <= range.y[1])
        });
    
        $list = $list.add($body);
    
        list = $list.map(function() {
            return this.nodeName + ' ' + this.className
        }).get();
        alert(list);
        return false;
    });​
    

    Original answer:

    This will give you an Array of the tag names including the span. Couldn’t quite tell if this is what you wanted.

    It uses .parents() along with .andSelf() to get the elements, then uses .map() with .get() to create the Array.

    Example: http://jsfiddle.net/9cFTG/

    var list;
    
    $('span').click(function() {
        list = $(this).parents().andSelf().map(function() {
            return this.nodeName;
        }).get();
        alert(list);
    });​
    

    If you just wanted the elements, not the tag names, get rid of .map() and .get().

    Or if you wanted to join the Array into a String using some sort of separator, just add .join(" ") after .get(), placing your separator inside the quotes.

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

Sidebar

Related Questions

I would like it that when the user clicks out of the #email-form div,
I would like to allow the user to click within my UserControl and drag
I would like to allow a user to click on a JList and if
I would like to order contents in a column when user clicks column header.
I would like to update some data with JSON when the user clicks on
When a user click a link, it would redirect them to a page, something
How can I force validation when user clicks button? One would think this should
I have done like if user click on the Button then the Sound is
My first application will just be a kind of launcher that I would like
I am passing a collection to UseSimpleClass. I would like UseSimpleClass property SimpleClassColCountChecked 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.