jQuery counter and adding a class but not to the first two!?

jQuery counter and adding a class but not to the first two!?
0.0 0

#1

Hi all,

I’m checking list items I have and if there are more than two then I add a class. This works fine but I want the class to start adding after the first two list items. I am not sure how to do that part - need to exclude adding the class to the first two. Here is my code:

$( document ).ready(function() {

  //Checking how many time the class appears
   var numItems = $('ul.singleMagazine li').length;

    if (numItems > 2 ) {
     alert(numItems);
  
    //Want to add this class but not to the first two - exclude first two list items and then add it to the rest. How to do it?
    $('ul.singleMagazine li').addClass("newClass");
  }
 });

Anyone know how I would do the excluding part?

Cheers :slight_smile:


#2

Ah I think I have done it and it has solved it but I want to see what you guys / gals come up with here is my solution:

$( " ul.singleMagazine li:nth-child(1), ul.singleMagazine li:nth-child(2)" ).removeClass( "newClass" );

#3

Anyone? I have another better solution but want to see if you guys can get it too?