smuggyuk wrote:
Given a ul hierarchy as below, I'm struggling to write a JQuery
selector that will give me a ul that contains only one li element,
that I can then add a class to, in this example the ul is the one that
contains the li "Only Child".

<ul>
<li><div><a href="#">First</a></div>
   <ul>
   <li><div><a href="#">Only Child</a></div></li>
   </ul>
</li>
<li><div><a href="#">Second</a></div></li>
</ul>

So far I've tried permuations on a theme of:
$("ul:has(li:only-child)").addClass("solo");
and
$("ul li:only-child").addClass("solo"); - this one gets close, but
adds the class to the li rather than the ul.

I'm sure this is possible, so any help gratefully received,
Chris

Not sure if it's doable with a selector, but you could try a custom filter:

$('ul').filter(function() {
    return $('>li', this).length == 1;
});


--Klaus

Reply via email to