I am trying to learn jQuery
for a new job offering I got and found myself getting a little confused with the different traversal commands it offers.
I want to print the name tags of each element, their depth, and their closing tags in order and was curious if there was a way to do this.
I have it working for descending elements, and elements with no children, but I cannot get the ascending closing tags for the elements.
Here is the html
<html id = "test">
<head>
</head>
<body>
<p> Hello World</p>
<div id = "div1"></div>
</body>
</html>
here is my jQuery Script
$(document).ready(function() {
domWalk();
function domWalk() {
$("html").find("*").addBack().each(function() {
console.log( $(this).parents().length +" <"+ this.nodeName + "> ");
if($(this).children() .length == 0){
console.log( $(this).parents().length +" </"+ this.nodeName + "> ");
}
});
};
});
Expected results
0<HTML>
1<HEAD>
1</HEAD>
1<BODY>
2<P>
2</P>
2<DIV>
2</DIV>
2<SCRIPT>
2</SCRIPT>
1</BODY>
0</HTML>
Actual results
0<HTML>
1<HEAD>
1</HEAD>
1<BODY>
2<P>
2</P>
2<DIV>
2</DIV>
2<SCRIPT>
2</SCRIPT>