tags:

views:

61

answers:

1

is there any way to generate css path given an element ?

i saw one for javascript.

+1  A: 

i don't understand why this one is downvoted, a good and legitimate question

here's an (oversimplified) example on how this could be done

<div id="a">
 <div class="b">
  <div><span></span></div>
 </div>
</div>


<script>
function getPath(elem) {
 if(elem.id)
  return "#" + elem.id;
 if(elem.tagName == "BODY")
  return '';
 var path = getPath(elem.parentNode);
 if(elem.className)
  return path + " " + elem.tagName + "." + elem.className;
 return path + " " + elem.tagName;
}

window.onload = function() {
 alert(getPath(document.getElementsByTagName("SPAN")[0]));
}
</script>
stereofrog