I'm looking to traverse a tree I've created using ul elements. I realize this can be done more easily using the appropriate selector, but I'd like to create a recursive function to do this.
The idea is to move down the tree and turn on all the sub lists while adding the class 'on' to li:first-child elements to turn the list on and highlight first items in each list. Here's how I'm going about it:
function showAllFirstDiv(topnav) {
$(topnav).show();
$(topnav + ' > li:first').addClass('on');
if ($(topnav + ' > li:first > ul:first').length)
{
var childUL = $(topnav + ' > li:first > ul:first');
showAllFirstDiv(childUL);
}
else
return true;
}
and I'm calling:
showAllFirstDiv('.top');
It turns on the .top ul and the first child ul ( .top > li > ul ) as well as turning .top > li:first-child on but stops there giving me the jQuery error 'Uncaught Syntax error, unrecognized expression: [object Object]'
I'm assuming I'm mixing up jQuery and JavaScript elements, but can't seem to figure out exactly what the problem is.
I'm new to JavaScript/jQuery and any help or other basic pointers are appreciated. Thanks.