I have a li element that I am selecting like this:
var parent_li = $(this).parent();
This element may or may not contain an ul element. I would like to add a child to the ul but if there is no ul element I would like to create and add it first.
I tried:
if( !parent_li.children('ul') )
{
//if the parent list doe not contain
//an UL element, then add one
parent_li.append($('<ul></ul>'));
}
However, I found out that parent_li.children(‘ul’) always returns an object even if it does not have any child ul elements.
So what do I need to do?
1 Answer