Siddu Siddu - 1 year ago 73
jQuery Question

How to add dynamically class to single < li> when using in Loop

I have created pagination using for-loop, so using JQuery how to add dynamically class to < li>. Here is my code:

echo "<ul class='pagination'>";
for($i=1; $i<=$number_of_pages; $i++)

echo "<li id='add_class'>"."<a href='first_year.php?p=$i'>".$i."</a>";

echo "</ul>";

Answer Source

you can do something like below. Consider you want add active class

  1. add class to first li element

    $("ul.pagination > li:first").addClass("active");

  2. add class to nth position element (for example position is 5, so you need to pass 4 as index)

    $("ul.pagination > li:eq(4)").addClass("active");

  3. If you want to add class to all element

    $("ul.pagination > li").addClass("active");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download