title: jQuery - Add class to last 2 elements only if length is more than 5
body: <p>How can I add CSS class to <strong>last 2 <code>&lt;li&gt;</code></strong> elements only the parent <code>&lt;ul&gt;</code> has more than <strong>5 <code>&lt;li&gt;</code></strong> elements.</p>
  
  <blockquote>
    <p><strong>HTML</strong></p>
  </blockquote>
  
  <pre><code>&lt;div id="mymenu"&gt;
      &lt;ul&gt;
          &lt;li&gt;Item 1&lt;/li&gt;
          &lt;li&gt;Item 2&lt;/li&gt;
          &lt;li&gt;Item 3&lt;/li&gt;
          &lt;li&gt;Item 4&lt;/li&gt;
          &lt;li&gt;Item 5&lt;/li&gt;
          &lt;li&gt;Item 6&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <blockquote>
    <p><strong>jQuery</strong></p>
  </blockquote>
  
  <pre><code>$('#mymenu ul li:last-child').prev('li').andSelf().addClass("red");
  </code></pre>
  
  <hr>
  
  <h2><a href="http://jsfiddle.net/ReddyPrasad/pkkb7f3b/" rel="nofollow">FIDDLE</a></h2>
  
  <hr>