Determining the depth of a nested HTML listMonday, March 26th, 2007 at 3:29 pm
I had this problem the other day that I needed to know how many nesting levels a menu has. After some failed attempts of walking the DOM and lots of comparisons I realised that it is actually a lot easier than I thought:
var nested = o.getElementsByTagName(‘ul’);
var allcount = 0;
var count = 1;
var kid = nested[i].getElementsByTagName(‘ul’);
kid = kid.getElementsByTagName(‘ul’);
You can check a testcase for treedepth here.
Any better way to do this?