Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
how to remove the class on clicking a button and adding new class

suppose there is a div with class "box" on clicking the button with class "button" I want to remove the class "box" from the div and add a absolutely new class "active" to the div. what will be the jQuery code of the following?

html

 <div class="box">
 <button class="button">button</button>
 </div>

and also how should I add the new class in CSS ? like normally we add.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use removeClass method to remove class and use addClass method to add class using jquery.

You can also chain method as:

box.removeClass("box").addClass("active");

const box = $(".box");

$(".button").on("click", function() {
  box.removeClass("box");
  box.addClass("active");
})
.box {
  background-color: red;
}

.active {
  background-color: yellowgreen;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box">
  <button class="button">button</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

The simplest jQuery technique is probably with toggle:

$('.button').click(function() {
  $(this).parent().toggleClass('box active');
});
.box {
  background: #ddd;
}

.active {
  background: pink;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box">
  <button class="button">button</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Perhaps the easiest way is to use the classList.replace method which you can use with both jQuery, and vanilla JS.

jQuery.

$('.button').click(function () {
  const parent = this.parentNode;
  parent.classList.replace('box', 'active');
});
.active { background: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box">
  <button class="button">button</button>
</div>

Vanilla JS.

const button = document.querySelector('button');
button.addEventListener('click', handleClick, false);

function handleClick() {
  const parent = this.parentNode;
  parent.classList.replace('box', 'active');
}
.active { background: red; }
<div class="box">
  <button class="button">button</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda