14

I am moving elements using javascript and I need to create a logic for the combinations happening during the drag/drops

I'm trying to get details from the elements, a CSS like selector could be also good, but dunno if it is possible.. (like copy-selector in chrome dev tools)

document.onmouseup = function(e){
    targetDest = e.target;
    //console.log('targetDest: ', targetDest);

    let 
    indexA = Array.from(targetCurr.parentNode.children).indexOf(targetCurr),
    indexB = Array.from(targetDest.parentNode.children).indexOf(targetDest);

    console.log(indexA, indexB);


    if(targetDest != targetCurr){
        if(targetDest == document.documentElement){
            console.log('document');
        }
        else if(targetDest == undefined){
            console.log('undefined');
        }
        else if(!targetDest){
            console.log('!dest');
        }
        else if(targetDest == null){
            console.log('null');
        }
        else if(targetDest == false){
            console.log('false');
        }
        else{
            console.log('else');
            //targetCurr.parentNode.insertBefore(targetDest, targetCurr);

            //console.log('...');
        }
    }else{
        console.log('itself');
    }


}
neoDev
  • 2,879
  • 3
  • 33
  • 66
  • What do you mean by `selector`? An element may match any number of selectors if you're referring to DOM queries. eg `*`, or `div` or `div.content` or `div#mainDiv` or `html > body > div#mainDiv.content` could all match the same element. – CollinD Feb 12 '17 at 04:55
  • @CollinD like the one used for CSS. In chrome dev tools I can copy this, but not sure how to do it in plain JavaScript – neoDev Feb 12 '17 at 04:56
  • 1
    You'd need to construct some selector that would meet your needs. Elements don't have a unique magic selector. Consider posting some sample markup and your expected output. That would help potential answerers. – CollinD Feb 12 '17 at 04:57
  • You can use [`querySelector()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector). In your case: `document.querySelector("div#mainDiv")`. – Spencer Wieczorek Feb 12 '17 at 04:59
  • What do you need the selector for? If you're talking about splitting it, I suspect that getting the selector isn't actually your problem, but your (not necessarily optimal) approach to the actual problem you're having... – Frxstrem Feb 12 '17 at 05:03
  • Check this: https://blog.davidvassallo.me/2019/09/20/a-javascript-reverse-css-selector/ - https://chromium.googlesource.com/chromium/blink/+/master/Source/devtools/front_end/components/DOMPresentationUtils.js – Marian07 Jan 03 '23 at 16:18

6 Answers6

17

Keep in mind that this will not necessarily uniquely identify elements. But, you can construct that type of selector by traversing upwards from the node and prepending the element you're at. You could potentially do something like this

var generateQuerySelector = function(el) {
      if (el.tagName.toLowerCase() == "html")
          return "HTML";
      var str = el.tagName;
      str += (el.id != "") ? "#" + el.id : "";
      if (el.className) {
          var classes = el.className.split(/\s/);
          for (var i = 0; i < classes.length; i++) {
              str += "." + classes[i]
          }
      }
      return generateQuerySelector(el.parentNode) + " > " + str;
}

var qStr = generateQuerySelector(document.querySelector("div.moo"));
alert(qStr);
body
<div class="outer">
  div.outer
  <div class="inner" id="foo">
    div#foo.inner
    <div class="moo man">
      div.moo.man
    </div>
  </div>
</div>

I wouldn't suggest using this for much besides presenting the information to a user. Splitting it up and reusing parts are bound to cause problems.

CollinD
  • 7,304
  • 2
  • 22
  • 45
  • 4
    As you mentioned, this approach fails for "look-alike siblings", e.g. ```
  • ...
  • ...
  • ...
  • ```. The solution is to use ```:nth-child(X)``` instead. – mikiqex Nov 12 '19 at 12:47
  • @mikiqex could you please post an answer with a code example ? – Mouradif Feb 19 '21 at 02:15
  • @KiJéy https://stackoverflow.com/a/66291608/1053562 There you go. – mikiqex Feb 21 '21 at 13:17
  • 1
    It works fine. However I had to add `.map(c=>c.trim()).filter(c=>c)` after `.split(/\s/)` to avoid getting empty class names. – Marinos An Dec 01 '22 at 18:15