Posted By

smoothdzion on 01/12/10


css jquery last first

Versions (?)

Who likes this?

4 people have marked this snippet as a favorite


jQuery - Add First and Last Class to LI

 / Published in: jQuery

Since :first-child and :last-child aren't compatible with all modern browsers I thought I'd post this snippet. It make automating the class creation easier too instead of having to remember when typing up your HTML or creating logic in your programming.

Rename "first-item" and/or "last-item" to whatever class you want to use. You can change the "li" part of the code to whatever element your want or even remove it altogether.

  1. $(function(){
  2. $("li:first-child").addClass("first_item");
  3. $("li:last-child").addClass("last_item");
  4. });

Report this snippet  


RSS Icon Subscribe to comments
Posted By: liange on June 21, 2011

I am in little confusion about the class you used here. Is it the predefined class or the user defined function.

white label seo

You need to login to post a comment.