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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:26:01+00:00 2026-05-18T11:26:01+00:00

For the following project I will be using PHP and jQuery. I have the

  • 0

For the following project I will be using PHP and jQuery.

I have the following code:

$('*').onclick(function(){

});

Once a user clicks on an element I want it to return the XPath location of that element.

What I want to do essentially is sort of a filtering system with a little help of jQuery.

PHP will retrieve a document from an outside source with PHP, using jQuery a user click on an element/relevant parts of that document. This jQuery function will return the XPath location of where the user clicked and is stored and associated with that document.

In the future if that document is updated, using the XPath, PHP will retrieve only the XPath that was selected by the user previously instead of the whole document.

I will need this jQuery function to also return element attributes such as the title in a <p> or href location in an anchor link.

Long story short: My question is, is this even possible in jQuery? Returning the XPath location of a selected element along with it’s attributes? I have no idea what this would look like in jQuery so any help would be appreciated.

Any other additional feedback would be great too. As far as I know I could be using the wrong tools… or something. Thanks.

  • 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-18T11:26:02+00:00Added an answer on May 18, 2026 at 11:26 am

    You can use the .parents() method, to get all ancestors of the clicked element.

    $(document).delegate('*','click',function(){
      var path = $(this).parents().andSelf();
      return false;
    });
    

    and then extract the info you want.

    Use the .delegate method to handle the event, so you do not need to attach it to all elements. And also use the .andSelf() method to include in the path the item that was clicked as well.


    A more complete example

    $(document).delegate('*','click',function(){
        var path = $(this).parents().andSelf();
        var xpath='/';
        for (var i = 0; i < path.length; i++)
        {
            var nd = path[i].nodeName.toLowerCase();
            xpath += '/';
            if (nd != 'html' && nd != 'body')
            {xpath += nd+'['+ ($(path[i-1]).children().index(path[i])+1) +']';}
            else
            {xpath += nd;}                    
        }
        alert(xpath);
        return false;
    });
    

    Example with a test html to play at http://www.jsfiddle.net/gaby/hsv97/1/


    update with id and class shown as well : http://www.jsfiddle.net/gaby/hsv97/2/

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

Sidebar

Related Questions

MSBuild 3.5 I have the following project structure: trunk/MainSolution.sln trunk/Build/MyBuild.Proj trunk/Library/... trunk/etc... So far,
Let's say we have a solution with the following structure: Project.DAL - Data access
I have a web application that comprises the following: A web project (with a
The following code should find the appropriate project tag and remove it from the
The following have been proposed for an upcoming C++ project. C++ Coding Standards, by
I have a project that has the following line in the additional includes section:
I have an Xcode project with the following group structure: ProjectName/ Classes/ class1.h class1.m
I have created an interface using jQuery UI Tabs, however one of my requirements
I'm trying to create a Zend Framework project using PHP 5.3.2 and Zend Framework
I'm aiming to create a feature in my latest project preferably using PHP. When

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.