Knowing if input is focused or not in real time

Asked

Viewed 1,648 times

0

Hello, I would like to make a condition in javascript where the input is focused, a label stay none and when the person clicks off it and "blurs", the label reappear. But I don’t know how to make this condition of checking real time, I’ve researched about

I know there’s a way to get the value of .focus = true/false, but I don’t know in real time. It has how to do this ?

function busca() {
  var lupa = document.getElementById("lupa");
  var busca = document.getElementById("espacobuscar");
  var input = document.getElementById("buscaprod");
  input.autofocus = true;
  busca.style.cssFloat = "left";
  busca.style.display = "block";
  lupa.style.display = "none";
}
<div id="espacobuscar" class="fl">
  <!-- Aqui é o input da busca -->
  <input type="text" id="buscaprod" onkeyup="proc_produto_catalog(this.value)" placeholder="Buscar" />
  <div id="ls_produto_catalogo"></div>
</div>
<img id="lupa" onclick="busca()" src="../_imagens/lupa.png" />
<!-- Aqui é uma lupa que quando clicada, aparece o input -->

<div class="fr">
  <label id="label_mobile" class="fl" for="span_ped">Tipo de Pedido: </label>
  <!-- Esse é o label que tem que desaparecer quando a busca estiver com foco -->
  <div id="tipo_ped">
    <span id="span_ped"></span>
  </div>
</div>

  • What is your code? What do you have so far?

  • I edited the post, put the code

1 answer

4


You can use the events phocus (when the element receives the focus) and Blur (when the element loses focus).

Example:

var teste = document.getElementById('teste');
var feedback = document.getElementById('feedback');

teste.addEventListener('focus', function() {
  feedback.hidden = true;
});

teste.addEventListener('blur', function() {
  feedback.hidden = false;
});
*[hidden] {
  display: none;
}
<input id="teste">
<div id="feedback">Input sem foco</div>

Or do this only with CSS using the pseudo class :focus:

.teste + .feedback {
  display: block;
}

.teste:focus + .feedback {
  display: none;
}
<input class="teste">
<div class="feedback">Input sem foco</div>

In the above example was used the adjacent element selector.

  • I’ll test that way, I’ll answer here again

  • I updated with a CSS-only example.

  • The return of those who didn’t! I came to give an answer only with CSS, but you had already solved :D

  • I tried both ways and it wasn’t

  • 1

    @hugocsl when adding the CSS-only version I thought you were already responding. hahahaha

  • @Gabrielarcuri you know how the selector works + ?? My examples are working, but to apply to your case you need to change or check if it’s what you really need. I just showed you a way to solve your problem. You need to adapt to your case

  • By its HTML structure is the JS version that will use.

  • How my elements are with id, I changed the . for # and the names teste and feedback for buscaprod and label_mobile

  • Using javascript gave this error

  • Dude, the concept works. The problem is your code. Look at you

  • I could see that it works, I saw here that somehow mine input and label are returning null. I’ll see what’s doing

  • I really appreciate your help

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.