Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
Is there a way to select an element by class even though the class has been removed?

At the start I don't want .screen-text-page2 visibile, only after .screen-text-button-page1 is clicked

$('.screen-text-page2').removeClass("screen-text-page2");

$('.screen-text-button-page1').click(function() {
  $('.screen-text-page2').addClass("screen-text-page2");
});
.screen-text-page2 {
  background: pink;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button class="screen-text-button-page1">Button</button>

<div class="screen-text-page2">Div with class screen-text-page2</div>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Short answer: Yes

The removeClass method is not a state... once you run it the class is removed and the element can accept any new class, including the same one.

If you want to re-add the class, then just add it again.

$("#ele").removeClass(".myClass");
$("#ele").addClass(".myClass");

is completely valid

about 4 years ago · Juan Pablo Isaza Denunciar

0

Yes, but because you're removing the class, you won't be able to select it again via $('.screen-text-page2'). But if you save the selected element as a variable, you can reference it again without needing to reselect it using the class.

let page2 = $('.screen-text-page2');
$(page2).removeClass("screen-text-page2");

$('.screen-text-button-page1').click(function() {
  $(page2).addClass("screen-text-page2");
});
.screen-text-page2 {
  background: pink;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button class="screen-text-button-page1">Button</button>

<div class="screen-text-page2">Div with class screen-text-page2</div>

Another way to do something like this is to add another class before removing .screen-text-page2, and then reselecting the element using this placeholder class. But in my opinion, that's just more steps than the above example.

$('.screen-text-page2').addClass("screen-text-page2-placeholder");
$('.screen-text-page2').removeClass("screen-text-page2");

$('.screen-text-button-page1').click(function() {
  $('.screen-text-page2-placeholder').addClass("screen-text-page2");
});
.screen-text-page2 {
  background: pink;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button class="screen-text-button-page1">Button</button>

<div class="screen-text-page2">Div with class screen-text-page2</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda