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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:12:58+00:00 2026-06-12T09:12:58+00:00

Running into a spot of trouble and basically trying to create a variable which

  • 0

Running into a spot of trouble and basically trying to create a variable which can be used as a selector. eg

$('a').click(function(){
   var selector = $(this).dompath();
});

HTML:

html
    body
        div
            div /div
        /div
       ul
         li
         li
       /ul
    div
        ul
           li
           li
           li hello world
        /ul
   /div
   body
html

this would then return something like

path = html body div ul li:contains('hello world')

then i could use this in a selector to select this div so if i did like

$(path).text() would return "hello world"

many 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-06-12T09:12:58+00:00Added an answer on June 12, 2026 at 9:12 am

    Perhaps something like this:

    function dompath( element )
    {
        var path = '';
        for ( ; element && element.nodeType == 1; element = element.parentNode )
        {
            var inner = $(element).children().length == 0 ? $(element).text() : '';
            var eleSelector = element.tagName.toLowerCase() + 
               ((inner.length > 0) ? ':contains(\'' + inner + '\')' : '');
            path = ' ' + eleSelector + path;
        }
        return path;
    }
    

    This modified a method from another question to go through, and add in the full text contents of the tag via a :contains() operator only if the tag has no children tags.

    I had tested with this method:

    $(document).ready(function(){
        $('#p').click(function() {
          console.log(dompath(this));
        });
    });
    

    Against this:

    <html>
        <body>
            <div>
                <div> </div>
            </div>
           <ul>
             <li></li>
             <li></li>
           </ul>
           <div>
             <ul>
               <li id="p">hi</li>
               <li></li>
               <li id="p2">hello world</li>
            </ul>
           </div>
       <body>
    <html>
    

    The results of clicking on p then get output as:

    html body div ul li:contains(‘hi’)

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

Sidebar

Related Questions

I'm running into some trouble with the copy_file function. My program is very simple,
I'm running into trouble accessing global variables when I make an AJAX call to
So i am running into a major issue. I am currently trying to use
Running into an issue, which most likely is a permission thing with Facebook, but
I keep running into an issue where Eclipse returns No AVD's Available when trying
Running into some weird issue, can't quite figure out what's wrong. No errors popping
I'm running into a blind spot with backbone.js. I've written this code and all
I am running into issues trying to use large objects in R. For example:
Running into a problem with opencv 2.4.0 findContours function constantly crashing. Unfortunately, it's been
running into a little issue, can't quite figure it out. I'm using jQuery on

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.