2

Usually, we use querySelectorAll('html:eq(0) > body:eq(0) > div.row:eq(0) > div.span4:eq(2) > p.p3:eq(2)') to get the DOM ELEMENT.

If I want to use the ELEMENT <p class="p3">target</p> with native javascript to get the selector string, what should I do?

For example:

<div class="row">
    <div class="span4">
        <p class="p1">p1_1<p>
        <br>
        <p class="p1">p1_2<p>
        <p class="p1">p1_3<p>
    </div>
    <br>
    <div class="span4">
        <p class="p2">p2_1<p>
        <p class="p2">p2_2<p>
        <br>
        <p class="p2">p2_3<p>
    </div>
    <p>stranger</p>
    <div class="span4">
        <p class="p3">p3_1<p>
        <br>
        <p class="p3">p3_2<p>
        <br>
        <p class="p3">target<p>
    </div>
<div>
<div class="row">
    <div class="span3">
        <p class="p3">p1<p>
        <br>
        <p class="p3">p2<p>
        <p class="p3">p3<p>
    </div>
<div>
<br>
<div class="row">
    <p>stranger</p>
<div>

Thses are what I think:

  1. Taking advantage of those attributes of the element <p class="p3">target</p>, I found these are useful:
    • attributes
    • nextElementSibling
    • previousElementSibling
    • parentElement
  2. Attribute => attributes: I can use it generate the string like div.className#idName, this is not hard to do.
  3. Attribute => nextElementSibling, previousElementSibling: I can use it generate the string like :eq(0).

What I met as a problem is the Step3, because if there are BR between two DIV, then this two ElementSibling will return BR, it means the BR is treated as a brother of this two div who has the same class, but that is not i want.

So I tried to consider use Step3.1:

3.1. Attribute => parentElement: Use element's parent to get the eq, who it is ? 0, 1, 2.....

But I find i cannot specify targeting element is which one, because he has so many same brothers have the same tagName, and className....

I would like to know if you guys have any good ideas?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Leyla Lee
  • 466
  • 5
  • 19
  • :eq() is not a CSS selector. Also, any arbitrary element is uniquely reachable by at least one selector. – BoltClock Apr 21 '16 at 15:28
  • Are you trying to select whichever element has the content "target" in it? It's a little unclear what exactly your goal is. – TylerH Apr 21 '16 at 15:29
  • @TylerH sorry, the element which content is "target", is just an example here, i want to create a function in the end, for any element – Leyla Lee Apr 21 '16 at 15:31
  • @BoltClock Thank your for your reply, i know there are some arbitrary elements, it is just an example, off course, in the end, this function is used for the uniq element – Leyla Lee Apr 21 '16 at 15:35
  • @LeylaLee So you want to select all elements that are the last child of their parent? Or specifically the last `

    ` of each group of `

    ` elements?

    – TylerH Apr 21 '16 at 15:36
  • @LeylaLee Oh, you want to select the element that has unique content? – TylerH Apr 21 '16 at 15:38
  • @TylerH: The OP wants to generate a selector string for an arbitrary element. Kind of like the "Copy Unique Selector" option in the DOM inspector. – BoltClock Apr 21 '16 at 15:40
  • @TylerH I want to generate a selector string for an arbitrary element. – Leyla Lee Apr 21 '16 at 15:46
  • @BoltClock, thank your for your replay, you got it! – Leyla Lee Apr 21 '16 at 15:46

1 Answers1

-3

If you are trying to find the element based on the html, in this case "target", you can do this:

var elms = document.querySelectorAll('p');
var targetElm;
for (var i = 0; i < elms.length; i++) {
   if (elms[i].innerHTML === 'target') {
       targetElm = elms[i];
   }
}
mac
  • 858
  • 1
  • 6
  • 11
  • No, the OP has an element and is trying to build a selector string for it. – BoltClock Apr 21 '16 at 15:36
  • @BoltClock It doesn't seem like she's defined a case that can be reduced to a unique selector. If it is unique content she is searching for, the above answer is valid. – mac Apr 21 '16 at 15:39
  • Thank you for your reply, mac. But if there are some other elements whose innerHTML is also 'taget'? But the other elements is not the same with the input of function.... – Leyla Lee Apr 21 '16 at 15:40
  • @LeylaLee you can apply a sub selection based on the document.querySelectorAll. I'm not sure I'm understanding your use case well enough to provide a selector here. Can you elaborate what you mean by "the other elements is not the same with the input of the function"? – mac Apr 21 '16 at 15:46
  • @mac sorry for my bad english T.T , ok for `example

    p1_1


    p1_2

    p1_3


    p2_1

    p2_2


    target

    stranger

    p3_1


    p3_2


    target

    `
    – Leyla Lee Apr 21 '16 at 15:48
  • @mac omg, the bad formate, sorry... What i got for the input of the function is the first target, but if we follow your function, it will also generate selector string for the second element which also have innerHTML 'target' – Leyla Lee Apr 21 '16 at 15:51
  • The OP is not interested in the fact that the element's content is "target". She has a reference to this element, and is looking for a way to build a selector string that is guaranteed to match this element and only this element in this DOM. Every element has at least one such selector, and in a naïve solution the majority of these will rely on liberal use of :nth-child(). – BoltClock Apr 21 '16 at 15:55
  • @BoltClock thank you for adding clarification. @Lelya if you already have a reference to the element, why not apply a unique attribute instead? `elm.setAttribute('data-id', Date.now());` or something similar. – mac Apr 21 '16 at 16:07
  • I think you are looking for the Xpath, but I'm not sure there is native functionality to generate this for you. However you can see this answer for a possible solution http://stackoverflow.com/questions/2661818/javascript-get-xpath-of-a-node – mac Apr 21 '16 at 16:08