title: jQuery: how to check if child is first and also last
body: <p>I am trying to set a custom style for elements that are</p>
  
  <ul>
  <li>ul - the first in many children (ul.first)</li>
  <li>last in many children (ul.last)</li>
  <li>the only child (ul.first.last or ul.only)</li>
  </ul>
  
  <p>HTML: </p>
  
  <pre><code>&lt;ul class="selector"&gt;
      &lt;li class="selector"&gt;some li
         &lt;ul class=""&gt;
            &lt;li&gt;some fancy li - the ul parent should have first class&lt;/li&gt;
         &lt;/ul&gt;
         &lt;ul class=""&gt;
            &lt;li&gt;some more li - the ul parent should have last class&lt;/li&gt;
         &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="selector"&gt;some second li
          &lt;ul class=""&gt;
            &lt;li&gt;some lonely li - the ul parent should have first and last classes&lt;/li&gt;
         &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>so I came up with a piece of jQuery hoping to do the trick</p>
  
  <pre><code>$('ul.selector li.selector &gt; ul').each(function () {
    if ($(this).is(':first') &amp;&amp; $(this).is(':last')) {
      $(this).addClass('first last');
    } else if ($(this).is(':first')) {
      $(this).addClass('first');
    } else if ($(this).is(':last')) {
      $(this).addClass('last');
    }
  });
  </code></pre>
  
  <p>Thanks for any suggestion.</p>
  
  <p>UPDATE - here is a fiddle you can play with, based on one of the answers: <a href="http://jsfiddle.net/qAtpe/" rel="nofollow">http://jsfiddle.net/qAtpe/</a></p>