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

167
Vistas
Simple javascript click to add class not working

I am trying to add / remove a class on an element that is clicked liek this

function myFunction() {
    this.classList.add("myclass");
}
#first {
  height: 100px;
  width: 100px;
  background: yellow;
  color: black;
}

.myclass {
    background: red;
    color: white;
}
<div id="first" onclick="myFunction(this)">

  Click
  
  <div class="second">
  </div>
  
  <div class="third">
  </div>
  
</div>

Why is this not working?

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

0

You need to pass reference this into function too like:

function myFunction(el) {
  el.classList.add("myclass");
}
#first {
  height: 100px;
  width: 100px;
  background: yellow;
  color: black;
}

.myclass {
  background: red!important;
  color: white!important;
}
<div id="first" onclick="myFunction(this)">

  Click

  <div class="second">
  </div>

  <div class="third">
  </div>

</div>

PS. add !important into css

about 4 years ago · Juan Pablo Isaza Denunciar

0

Pass the this to the defined function too and check the existence of the class. Try this.

function myFunction(el) {
  if(!el.classList.contains("myclass")) {
    el.classList.add("myclass");
    console.log("added");
  } else {
    el.classList.remove("myclass");
    console.log("removed");
  }
}
#first {
  height: 100px;
  width: 100px;
  background: yellow;
  color: black;
}

.myclass {
    background: red;
    color: white;
}
<div id="first" onclick="myFunction(this)">

  Click
  
  <div class="second">
  </div>
  
  <div class="third">
  </div>
  
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

It doesn't work because this for inline handlers works differently. You can use .call, and that works... but that's still not good.

function myFunction() {
  this.classList.add("myclass");
}
#first {
  height: 100px;
  width: 100px;
  background: yellow;
  color: black;
}

.myclass {
  background: red !important;
  color: white;
}
<div id="first" onclick="myFunction.call(this)">
  Click
  <div class="second">
  </div>
  <div class="third">
  </div>
</div>

You should avoid inline script altogether and also avoid id selectors in CSS.

Change your id selector to a class selector and change your inline handler to an event listener.

Also, stray strings like "content" are a real pain as your project grows in size. Wrap them in a <span>

const myButton = document.querySelector(".first");

myButton.addEventListener("click", ({
  target
}) => target.classList.add("myclass"))
.first {
  height: 100px;
  width: 100px;
  background: yellow;
  color: black;
}

.myclass {
  background: red;
  color: white;
}
<div class="first">
  <span>Click</span>
  <div class="second"></div>
  <div class="third"></div>
</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