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

212
Visualizações
Returning focus to input after clicking button not working

This has got to be really simple, but I just didn't find any answer. I have a simple menu to enter special characters into a search text box. Its doing its job but if i click a character the menu will (blur) and the focus is not returned to the field. As long as the cursor is in the field and/or I click on the characters menu, I want the menu to remain visible. On the event listner for the menu buttons, the first and last lines work. The text is appended to the search box, and hello world logged to the console. But the menu does not show, and the text box does not regain the focus. What is wrong here?

problem demo here

let searchInput = document.querySelector('#search-input');
let charsMenu = document.querySelector('.charsmenu');

// add event listeners to textbox                                                                                                                                             
searchInput.addEventListener('focus', function() {
  toggleDisplay(charsMenu, true);
});

searchInput.addEventListener('blur', function() {
  toggleDisplay(charsMenu, false);
});

function toggleDisplay(element, show) {
  if (show)
    element.style.display = 'block';
  else
    element.style.display = 'none';
}

let listItems = document.querySelectorAll('.charsmenu li');

listItems.forEach((item, index) => {
  item.addEventListener('mousedown', (event) => {
    searchInput.value += `${event.currentTarget.innerHTML}`;
    toggleDisplay(charsMenu, true);
    searchInput.focus();
    console.log("Hello world!");
  });
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Chances are, after mousedown is fired, the <li> element is going to steal focus away from the input element. Therefore you will want to defer setting focus on the input element at the end of the call stack, i.e.:

window.setTimeout(() => searchInput.focus(), 0);

See proof-of-concept below:

let searchInput = document.querySelector('#search-input');
let charsMenu = document.querySelector('.charsmenu');

// add event listeners to textbox                                                                                                                                             
searchInput.addEventListener('focus', function() {
  toggleDisplay(charsMenu, true);
});

searchInput.addEventListener('blur', function() {
  toggleDisplay(charsMenu, false);
});

function toggleDisplay(element, show) {
  if (show)
    element.style.display = 'block';
  else
    element.style.display = 'none';
}

let listItems = document.querySelectorAll('.charsmenu li');

listItems.forEach((item, index) => {
  item.addEventListener('mousedown', (event) => {
    searchInput.value += `${event.currentTarget.innerHTML}`;
    toggleDisplay(charsMenu, true);
    window.setTimeout(() => searchInput.focus(), 0);
  });
});
.wrapper {
  display: flex;
}

.charsmenu {
  display: none;
  list-style: none;
}

.charsmenu li {
  float: left;
  border: 1px solid black;
  padding: 16px;
}
<div class="wrapper">
  <ul class="charsmenu">
    <li>a</li>
    <li>b</li>
    <li>c</li>
  </ul>

  <input type="text" id="search-input" />
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

As Terry said, the focus is being stolen anyway.
My solution is just add preventDefault() before the focus setting:

listItems.forEach((item, index) => {
  item.addEventListener('mousedown', (event) => {
    searchInput.value += `${event.currentTarget.innerHTML}`;
    toggleDisplay(charsMenu, true);
    event.preventDefault();
    searchInput.focus();
  });
});

By the way, you can have event listener on a <UL> instead of each <LI>.

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