This should be a nice little puzzle, and hopefully solvable by jQuery. Here is a self explanatory jsFiddle. Note that I am looking for a generic solution to traverse the elements of interest in the dom based on where they are in the dom tree. I provide this reduced case as an example, so you can test your solutions against it and it is also easier to understand.
The DOM:
<div class="element" value="Hi5!">
<div class="element" value="Zero"></div>
<div class="junk" value="no no!"></div>
<div class="element" value="Four">
<div class="element" value="One"></div>
<div class="element" value="Three">
<div class="element" value="Two"></div>
</div>
</div>
<div class="element" value="Five"></div>
</div>
The code I'v got which prints elements in an undesired order:
$(document).ready(function(){
console.log("ready");
$(".element").each(function(index, item){
console.log(index + " | ", item.getAttribute('value'));
});
});
The current output:
ready
0 | Hi5!
1 | Zero
2 | Two
3 | One
4 | Four
5 | Three
6 | Five
How can I traverse and print the values of the nested div elements above in a postorder fashion?
Edit: thanks to @zshooter for providing a solution and pointing out a mistake in my question. Here is my updated jsFiddle ordered properly in postorder.