Created
May 23, 2018 06:16
-
-
Save nikolayvitaev/ad91e655154b26b2eac6c357ae87a480 to your computer and use it in GitHub Desktop.
JQuery get path. Return selector path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.extend({ | |
getPath: function () { | |
let path, node = this; | |
while (node.length) { | |
let realNode = node[0], name = realNode.localName; | |
if (!name) break; | |
name = name.toLowerCase(); | |
let parent = node.parent(); | |
let sameTagSiblings = parent.children(name); | |
if (sameTagSiblings.length > 1) { | |
let allSiblings = parent.children(); | |
let index = allSiblings.index(realNode) + 1; | |
if (index > 1) { | |
name += ':nth-child(' + index + ')'; | |
} | |
} | |
path = name + (path ? '>' + path : ''); | |
node = parent; | |
} | |
return path; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment