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

207
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

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