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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:03:52+00:00 2026-06-04T01:03:52+00:00

To get the names of all parent elements? $(document).on(‘click’, function(e){ var parents = $(e.target).parents().map(function(){

  • 0

To get the names of all parent elements?

 $(document).on('click', function(e){
    var parents = $(e.target).parents().map(function(){ 
        var $this = $(this),
            $id = $this.prop('id') ? '#'+$this.attr('id') : '',
            $class = $this.prop('class') ? '.'+$this.attr('class') : ''; 
        return this.tagName.toLowerCase()+$id+$class;
        }).get().join(" < ");
    prompt('The path to the element, Luke:',parents);
 });

FIDDLE with example.

EDIT: Nice! Thanks, guys! Updated FIDDLE.

  • 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-06-04T01:03:54+00:00Added an answer on June 4, 2026 at 1:03 am

    It works well, I’ve run some tests and none got to improve your current method.

    Additionally to the suggestion provided by gion_13, I would suggest reversing the order of displaying the elements!

    Depending on your goal, it might have a better readability from HTML to clicked element, by using .reverse():

    $(document).on('click', function(e){
      var parents = $(e.target).parents().map(function(){
    
        var $this  = $(this),
            $id    = $this.prop('id') ? '#'+$this.attr('id') : '',
            $class = $this.prop('class') 
                       ? '.'+$.trim($this.attr('class')).replace(/\s+/g,'.') 
                       : '';
    
        return this.tagName.toLowerCase()+$id+$class;
    
      }).get().reverse().join(" > ");
    
      prompt('The path to the element, Luke:',parents);
    });
    

    The Fiddle example here!

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

Sidebar

Related Questions

Is there a way to get the names of all the javascript function executed
I am trying to get the names of all first level directories under given
I am developing an application. I want to get the names of all .xml
Directory.GetFiles(targetDirectory); Using the above code we get the names( i.e full path) of all
How can I get all products from customers1 and customers2 include their customer names?
how do you get the list of all the computer names/ip address on your
How could I write code to get all the action names from a controller
Okay first of all, I admit I took elements from this question here: How
My question is: How can I get elements directly under a specific parent element
I try to get the name of executable name of all of my launched

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.