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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:24:39+00:00 2026-06-11T15:24:39+00:00

I need to generate a value of element based on the level in the

  • 0

I need to generate a value of element based on the level in the DOM. e.g. If I have a click event at document level (i.e. listening to any clicks on the pages) and the rendered HTML like this, it needs to traverse through DOM until you see "ID" attribute populated.

<div id="someid">
<div>
    <span>
        <p>
            <a href="javascript:void(0);">
                <span>
                    <strong>Googlie</strong> 
                </span>
            </a>                
        </p>
    </span>
</div>

When I click on "Googlie", it should generate something like this:
someid__div_span_p_a_span_strong

Issue:
I do have some code to do this but there is a problem with it. In above, what if I have two elements, right after another as shown below. How do I generate an unique value for strong element? Can I have something without using childNodes location? The childNodes value changes if someone changes the white space between the elements.

...<span>
<strong>Googlie</strong> <br/> <strong>Bingie</strong>
</span>...

    document.onclick = function(obj) {
    var node = obj.target;
    var res = [];
    var etA = "";
    res[0] = false;
    while (!res[0]) {
        res = GetIdFor(node);
        if (!res[0]) {
            var end = node.parentNode.childNodes.length;
            for (var i = 0; i < end; i++) {
                var j = node.parentNode.childNodes[i];
                if (j.nodeType === 1 && j === node) {
                    break;
                }
            }
            etA = "_" + node.tagName + etA;
            node = node.parentNode;
        }
        else {
            etA = res[1] + "_" + etA;
        }
    }
    alert(etA);
};
function GetIdFor(elem) {
    var res = [];
    res[0] = false;
    var d = elem.getAttribute("ID");
    if (d !== "undefined" && d !== null) {
        res[0] = true;
        res[1] = d;
    }
    return res;
}

Now, I know jQuery can help here but that’s not THE point. 🙂 Thoughts? Do I not have any other choice but using childNodes level in order to generate this value?

  • 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-11T15:24:40+00:00Added an answer on June 11, 2026 at 3:24 pm

    I adapted this answer from Here. The author suggests putting [#] as a sibling number so that you can differentiate between siblings, e.g. someid__div_span_p_a_span_strong[0]

    Working jsFiddle Here.

    function elementPath(element)
    {
        return $(element).parents().andSelf().map(function() {
            var $this = $(this);
            var tagName = this.nodeName;
            if ($this.siblings(tagName).length > 0) {
                tagName += "[" + $this.prevAll(tagName).length + "]";
            }
            return tagName;
        }).get().join(".").toLowerCase();
    }
    
    $(document).ready(function() {
        $(document).click(function(e) {
            e.stopPropagation();
            window.alert(elementPath(e.target));
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tool that generate entities I need to generate a samlpe value
I need to generate css based on client queries. I use the @import url()
I have a Flex application and need to generate a menu programatically, it will
I am currently working on a project where I need to generate multiple values
I need to generate a file for Excel, some of the values in this
I need to validate the text box value from the generated html table (grid)
I need generate action links outside controllers. I can use Html.Action in Views, Url.Action
I need to generate a couple of objects from lists in Javascript. In Python,
I need to generate such pattern for my input, for example: +375 29 911-91-91
I need to generate report that will show number of new / changed rows

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.