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

92
Visualizações
hide div without clicking away

I have the following code that hides a div when there is anything typed in a textbox.

 <script type="text/javascript" charset="utf-8">
    $('#email').change(function () {
      var len = $('#email').val().length;
      if (len > 0) {
          $("#user_accounts").fadeOut(1)
      } else {
          $("#user_accounts").fadeIn(1)

      }
    });
    </script>

This is working but it only works after you click away from the textbox and not when you start typing. I wanted to see if there is a way to execute this code when you start typing and not just when there is text in the field and you click away.

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

0

It's quite easy, here's the code:

document.getElementById('myInput').addEventListener('keyup', () => {
  if (document.getElementById('myInput').value.length > 0) {
    document.getElementById('myDiv').style.display = 'none';
  } else {
    document.getElementById('myDiv').style.display = 'block';
  }
})
#myInput {
  width: 200px;
  height: 35px;
  padding: 0 10px;
  background: #333;
  color: #fff;
  border: 0;
  outline: 0;
  font-family: arial;
}

#myInput::placeholder {
  color: #ccc;
}

#myDiv {
  width: 220px;
  height: 70px;
  margin-top: 20px;
  line-height: 70px;
  text-align: center;
  font-family: arial;
  background: red;
  color: #fff;
}
<html>

<body>

  <input id='myInput' type="text" placeholder="Type here!">

  <div id='myDiv'>Can you see me?</div>

</body>

</html>

Basically using

document.getElementById('myInput').addEventListener('keyup', () => { // your function }

you're calling a function everytime someone types inside the input (actually the function is called only when you realese a key) and then you just check for the input's value length inside of the function and hide or not the div based on the length.

Hope It's what you're looking for, if you have any question let me know.

about 4 years ago · Juan Pablo Isaza Relatório

0

According to the documentation, for <input type="text">, the change event only triggers after the element loses its focus (e.g. clicking away).

Unlike change event, input event is fired every time the value of the element changes. Therefore, it fits better to your usecase.

Your code will look like this:

$('#email').on('input', function() {
  var len = $('#email').val().length;
  if (len > 0) {
    $("#user_accounts").fadeOut(1)
  } else {
    $("#user_accounts").fadeIn(1)

  }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

for cleaner code, I would recommend using inline HTML and instead of onchange, I would actually use onkeyup (as other commentators noted) this event would call a named function which would be binded to the event.

as follows:

HTML

<element onkeyup="handleInputKeyup">...</element>

JS

const handleInputKeyup = (event) => {//do something with the event here};

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