How to add a class to a div when it is active, so it changes color?

Asked

Viewed 119 times

2

I’m making a Bootstrap-based website, where I have a portfolio zone, where thumbnails open a collapsible DIV. I need that when a collapsible DIV is open, the respective thumbnail changes its CSS formatting, in this case color. I tried to add a class to this so-called ". cliente_active" through my Javascript, but since I am not an expert in the latter language, I did not succeed. I’d appreciate it if you could help me.

My prototype:

FIDDLE

1 answer

7


I was very close, I made a change and added a line to remove classes that had been added to other elements

Forehead like this:

 $('.panel-heading').on('click', function () {
     var self = this;
     $('.cliente_activo').removeClass('cliente_activo'); // remover a classe em todos os elementos que a tiverem
     $(this).addClass('cliente_activo'); // adicionar a a classe ao elemento que recebeu o clique
     if (!bigMedia) {
         $('.panel-collapse').slideUp();
         $(this).parent().next().slideToggle('collapse');
         $(this).addClass('cliente_activo');
     } else {
         if (conteudo) {
             conteudo.slideToggle(function () {
                 $(this).remove();
                 abrirFechar(self);
                 // retirei daqui
             })
         } else abrirFechar(self);
     }

 });

Example

You could also use the aberto that you already have in the code, example

  • Thanks, only you come to help me! : D Now it has two bugs, in which when we close the class the div does not return to what it was initially. And in small screen mode not when we have a div open, when we click the respective button to close it, it closes and reopens.

  • 1

    @user3216077: here it is: http://jsfiddle.net/qN46N/ that’s the way it should be?

  • @user3216077, already now a suggestion: CSS Transitions for colors? :) http://jsfiddle.net/qN46N/1/

  • 1

    Yes it is. Thank you very much! D

  • I discovered now some bugs that happen when resizing the browser with an open div. When it goes from a larger dimension to a smaller one, the div closes and the color of the "button" is changed. And when you resize with an open div to a larger dimension (which will most often happen) the open div is on top of the others, and you can’t close, changing the button only by color. Thank you so much for your help once again.

  • 1

    @user3216077, and what about so: http://jsfiddle.net/H8kP2/

  • 1

    No more! 5 stars! Thank you! D

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.