element's absolute position in DOM Tree


/ Published in: JavaScript
Save to your folder(s)

a function that returns the absolute position in DOM tree. jQuery based


Copy this code and paste it in your HTML
  1. myId = function(me){ return me.id ? '#' + me.id : '' }
  2. myTag = function(me){ return me.tagName ? me.tagName.toLowerCase() : '' }
  3. myClass = function(me){ return me.className ? '.' + me.className.split(' ').join('.') : '' }
  4.  
  5. breadcrumbs = function(me){
  6. var path = [myTag(me) + myId(me) + myClass(me)];
  7. $(me).parents().each(function() {
  8. path[path.length] = myTag(this) + myId(this) + myClass(this);
  9. });
  10. return path.join(' < ');
  11. }
  12.  
  13. $('body').click( function(){
  14. alert( breadcrumbs(this) );
  15. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.