Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getDomPath(el) {
- var stack = [];
- while ( el.parentNode != null ) {
- console.log(el.nodeName);
- var sibCount = 0;
- var sibIndex = 0;
- for ( var i = 0; i < el.parentNode.childNodes.length; i++ ) {
- var sib = el.parentNode.childNodes[i];
- if ( sib.nodeName == el.nodeName ) {
- if ( sib === el ) {
- sibIndex = sibCount;
- }
- sibCount++;
- }
- }
- if ( el.hasAttribute('id') && el.id != '' ) {
- stack.unshift(el.nodeName.toLowerCase() + '#' + el.id);
- } else if ( sibCount > 1 ) {
- stack.unshift(el.nodeName.toLowerCase() + ':eq(' + sibIndex + ')');
- } else {
- stack.unshift(el.nodeName.toLowerCase());
- }
- el = el.parentNode;
- }
- return stack.slice(1); // removes the html element
- }
- //Usage:
- var path = getDomPath(document.querySelector('#main_passwd'));
- console.log("document.querySelector('",path.join(' > '),"');");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement