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

217
Visualizações
How to replace the entire class within the element in HTML?

I have a HTML like the below,

var divId = document.getElementById('a1');
$(divId).find('#b1').className = "red";
.green {
  background-color: green;
  color: #ccc;
}

.red {
  background-color: red;
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="a1">
  <button id="b1" class="green">green</button>
</div>

<div id= "a2">
  <button id ="b2" class="red">red</button>
</div>

How to replace the class Green with class red ?

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

0

You are mixing up jQuery and JavaScript here.

You can use className in a JavaScript referenced element. To change the class of a jQuery referenced element you should use .addClass(). Also, since the attribute id is unique in a DOM, simply specifying the id in the selector is enough.

$('#b1').addClass('class', 'red');

jQuery Solution:

$('#b1').addClass('red');
.Green {
  background-color: green;
  color: #ccc;
}
.red {
  background-color:red;
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id= "a1">
    <button id ="b1" class = "Green">Green</button>
</div>

<div id= "a2">
    <button id ="b2" class = "red">Red</button>
</div>

JavaScript Solution: Instead of using className, I will suggest you to use DOMTokenList.add() to add/remove class to an element.

var divId = document.getElementById('b1');
divId.classList.add('red');

var divId = document.getElementById('b1');
divId.classList.add('red');
.Green {
  background-color: green;
  color: #ccc;
}
.red {
  background-color:red;
  color: #fff;
}
<div id= "a1">
    <button id ="b1" class = "Green">Green</button>
</div>

<div id= "a2">
    <button id ="b2" class = "red">Red</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

$(divId).find returns a jQuery object.

className is a property of HTML Element in vanilla JavaScript.

You do one of the following

  • the jQuery prop method, to set a property on a jQuery object.
  • the jQuery addClass method, to add a class on a jQuery object.
  • convert the jQuery object to an Element (see SO answer)
  • bypass jQuery itself & use querySelector instead.
about 4 years ago · Juan Pablo Isaza Relatório

0

This will work for you:

$("#a1 #b1").removeClass("Green");
$("#a1 #b1").addClass("red");
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