views:

72

answers:

4

How can I add severeal classes to ul list? I dont want to add it to every li items but jsut some of them.

+1  A: 

Well, to add 3 classes to every li in a ul, you'd do:

$("#myUl > li").addClass("class1 class2 class3");

Since you don't specify any criteria for the li elements you want to get the additional classes, I'm forced to guess:

$("#myUl > li:even").addClass("class1 class2 class3");

That will add the three classes to every other li (the even numbered elements in a zero-indexed array, to be more precise).

Good luck.

inkedmn
Ah well, here's better explanition:<ul><li>something1</li><li>something2</li><li>something3</li><li>something4</li><li>something5</li></ul>And now I want to add class every li after the second one (3rd, 4th and 5th li element). I know how to add class but how can i make it to repeat in each li. Thats my problem really.
mkoso
You should add this comment as part of your question and format the code properly if you're still interested in an answer.
inkedmn
+1  A: 

Your question isn't detailed enough, but in general you'll want to set classes on the specific li elements as needed.

For example:

<ul>
<li class="first winter thirty-one">January</li>
<li class="winter">February</li>
<li class="spring thirty-one">March</li>
<li class="spring">April</li>
<li class="spring thirty-one">May</li>
<li class="summer">June</li>
<li class="summer schools-out thirty-one">July</li>
<li class="summer schools-out thirty-one">August</li>
<li class="fall">September</li>
<li class="fall thirty-one">October</li>
<li class="fall">November</li>
<li class="last winter thirty-one">December</li>
</ul>
lupefiasco
Oh yes, sorry. I need to know how to do it with jquery. See last answers comments for more details.
mkoso
A: 

I want to add classes with jQuery. Like this:

ul>

  • something1
  • something2
  • something3
  • something4
  • something5
  • And now I want to add class every li after the second one (3rd, 4th and 5th li element). I know how to add class but how can i make it to repeat in each li. Thats my problem really.

    mkoso
    +1  A: 

    There are several ways to access a specific index of elements.

    The :eq() selector can access a single index:

    $('li:eq(1)')
    

    http://api.jquery.com/eq-selector/

    The .nextAll() function returns all following siblings of each element in the set of matched elements:

    $('li:eq(2)').nextAll();
    

    http://api.jquery.com/nextAll/

    The .prevAll() works the same way but returns the previous siblings:

    $('li:eq(2)').prevAll();
    

    http://api.jquery.com/prevAll/

    David