0

I'm trying to modify an element class at a specified index, but it doesn't work for some reason

$('.pagination__next').click(function(){
    $('.pagination__page').removeClass('active');
    $('.pagination__page').eq(2).addClass('active');
});
.active {
  color: red;
}
<nav class="pagination col-md-12">
    <ul class="pagination__list">
        <li><a rel="prev" href="#" class="pagination__previous btn-squae disable">&#8249;</a></li>
        <li><span class="pagination__page btn-squae active">1</span></li>
        <li><a href="#" class="pagination__page btn-squae">2</a></li>
        <li><a href="#" class="pagination__page btn-squae">...</a></li>
        <li><a href="#" class="pagination__page btn-squae">14</a></li>
        <li><a rel="next" href="#" class="pagination__next btn-squae">&#8250;</a></li>
    </ul>
</nav>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
Faisal
  • 3
  • 3
  • 2
    What makes you think it doesn't work? What happens? What do you expect to happen instead? Your code will clearly add the class `active` to the third (!) element with `.pagination__page`. – T.J. Crowder Feb 16 '19 at 10:39
  • I tested it and it does not add the class active to third element – Faisal Feb 16 '19 at 10:41
  • The code in the question does. See the edit to the question, which just copies your code into a Stack Snippet and makes the `active` class visible. – T.J. Crowder Feb 16 '19 at 10:42
  • I suspect you're running your code before the elements are in the DOM. See the linked question's answers for what to do about that. Happy coding! – T.J. Crowder Feb 16 '19 at 10:43
  • I don' t think this is the case, when I remove the function eq() the code works perfectly fine – Faisal Feb 16 '19 at 11:05
  • If so, please update the question with a [mcve] demonstrating the problem in a Stack Snippet (since the current one doesn't) and ping me. But if those elements are in the DOM when that code runs, it definitely adds the class to the third element, as you can see in the current snippet. – T.J. Crowder Feb 16 '19 at 11:15

0 Answers0