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

201
Vistas
How do I make my javascript code for adding a new class to HTML more responsive?
<div class="control">
  <input
    type="email"
    id="email"
    class="email-address"
    onblur="getValEmail()"
  />
  <label for="email">Email</label>
</div>
<div class="control box">
  <textarea id="message" onblur="getValMsg()"></textarea>
  <label for="message">message</label>
</div>



function getValEmail() {
  const valEmail = document.getElementById("email");

  valEmail.addEventListener("blur", function () {
    if (valEmail.value) valEmail.parentElement.classList.add("filled");
    else valEmail.parentElement.classList.remove("filled");
  });
}

function getValMsg() {
  const valMsg = document.getElementById("message");

  valMsg.addEventListener("blur", function () {
    if (valMsg.value) valMsg.parentElement.classList.add("filled");
    else valMsg.parentElement.classList.remove("filled");
  });
}


.filled label,
input:focus + label,
textarea:focus + label {
  top: 0;
  font-size: 1.2rem;
}

These are HTML, Javascript and CSS codes in order. What I'm trying to achieve here is that once user writes email address/message, labels that are inside of input/textarea go up. So, when input/textarea are filled up, labels should disappear to the top. It does its job but not responsively. "filled" class is added after a couple of more clicks happen, which means that labels only go up to the top after I click the box again.

enter image description here

Result looks like this.

enter image description here

This is only achieved after a few more clicks in the box.

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

You don't need js here at all. The :placeholder-shown pseudo-class will do what you actually need to achieve (note the non-empty placeholder attrs required for chrome):

.control {
  position: relative;
  margin-top: 20px;
}

input+label,
textarea+label {
  position: absolute;
  left: 0;
  top: 0;
  transition: 300ms;
}

input:focus+label,
input:not(:placeholder-shown)+label,
textarea:focus+label,
textarea:not(:placeholder-shown)+label {
  top: -20px;
}
<div class="control">
  <input type="email" id="email" class="email-address" placeholder=" " />
  <label for="email">Email</label>
</div>
<div class="control box">
  <textarea id="message" placeholder=" "></textarea>
  <label for="message">message</label>
</div>

Here's a codepen.

about 4 years ago · Santiago Gelvez Denunciar

0

In your css use: transform:translateY(-2em) change accordingly to your adjustment.

about 4 years ago · Santiago Gelvez 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