How do I add class to the next element when I click the button?

10

I have a list with several items, and just above this list will have two buttons, "next" and "previous". I wanted the user to click next, for example, to add a different class to the next item in the list and remove the class from the previous item.

I know what I can do using next().addClass of jQuery but I could not.

    
asked by anonymous 03.02.2014 / 19:02

5 answers

5

You can use this:

$('button#anterior').click(function () {
    $('.minhaClasse').removeClass('minhaClasse').prev().addClass('minhaClasse');
    if (!$('.minhaClasse').length) {
        $('li:last').addClass('minhaClasse')
    }
});
$('button#proximo').click(function () {
    $('.minhaClasse').removeClass('minhaClasse').next().addClass('minhaClasse');
    if (!$('.minhaClasse').length) {
        $('li:first').addClass('minhaClasse')
    }
});

Example

    
03.02.2014 / 19:25
4

Your solution proposal (using next and addClass ) is correct. Here is a simple example (without checking for boundary conditions - like the first or last element):

$(".selecionado")
    .removeClass("selecionado")
    .next()
    .addClass("selecionado");

Example in jsFiddle .

P.S. A solution (not necessarily the cleanest) to test the boundary cases would check at the end of the code if "someone" is selected and if not, choose one as default (the last one, if you want it " front, "or the first if you want it to" go around "). Examples:

if ( $(".selecionado").length == 0 )
    $("li:last").addClass("selecionado");

Stopping example , example giving the turn .

    
03.02.2014 / 19:08
3

Something like this?

$('.next').on('click', function() {

    $('ul>li.active').removeClass('active').next('li').addClass('active');

});

$('.prev').on('click', function() {

    $('ul>li.active').removeClass('active').prev('li').addClass('active');

});
    
03.02.2014 / 19:08
2

The path is the same, but beware the .next() takes the next element, ie:

<script>
  $('.primeira').next();
</script>
<div class="primeira"></div>
<div class="segunda"></div>

The script takes the object div segunda .

But if so,

<script>
  $('.primeira').next();
</script>
<div class="primeira"></div>
<div class="itens">
    <div class="segunda"></div>
<div>

The object takes the class itens and not the second. You would have to get .next() after using .children()

link

    
03.02.2014 / 19:14
1

See if you understand logic.

$(document).ready(function(){
    var anterior  = $('.btnAnterior');
    var proximo   = $('.btnProximo');
    var itemLista = $('.lista li:first-child');
    var qtdItens  = $('.lista li').length;
    var itemNum   = 1;
    var itemAtual = 1;

    $.each('.lista li', function(){
        $(this).addClass('item'+itemNum);
        itemNum++;
    })        

    proximo.click(function(){
        $('item'+itemAtual).removeClass('classeRemovida');
        $('item'+itemAtual+1).addClass('classeAdicionada');
        itemAtual = itemAtual + 1;
    })

    anterior.click(function(){
        $('item'+itemAtual).removeClass('classeRemovida');
        $('item'+itemAtual-1).addClass('classeAdicionada');
        itemAtual = itemAtual - 1;
    })
});
    
03.02.2014 / 20:08